• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (WebHttpBodyStream)
2
3Represents the body of the data being sent in POST and PUT requests. It accepts data of the BYTES, FILE, BLOB, and CHUNKED types. Note that other APIs in this class can be called only after [initialize](#initialize12) is called successfully.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
8>
9> - The initial APIs of this class are supported since API version 12.
10>
11> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer.
12
13## Modules to Import
14
15```ts
16import { webview } from '@kit.ArkWeb';
17```
18
19## initialize<sup>12+</sup>
20
21initialize(): Promise\<void\>
22
23Initializes this **WebHttpBodyStream** instance.
24
25**System capability**: SystemCapability.Web.Webview.Core
26
27**Return value**
28
29| Type  | Description                     |
30| ------ | ------------------------- |
31| Promise\<void\> | Promise used to return the result.|
32
33**Error codes**
34
35For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
36
37| Error Code| Error Message                             |
38| -------- | ------------------------------------- |
39| 17100022 | Failed to initialize the HTTP body stream. |
40
41**Example**
42
43```ts
44// xxx.ets
45import { webview } from '@kit.ArkWeb';
46import { BusinessError } from '@kit.BasicServicesKit';
47import { buffer } from '@kit.ArkTS';
48import { WebNetErrorList } from '@ohos.web.netErrorList'
49
50@Entry
51@Component
52struct WebComponent {
53  controller: webview.WebviewController = new webview.WebviewController();
54  schemeHandler: webview.WebSchemeHandler = new webview.WebSchemeHandler();
55  htmlData: string = "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>";
56
57  build() {
58    Column() {
59      Button('postUrl')
60        .onClick(() => {
61          try {
62            let postData = buffer.from(this.htmlData);
63            this.controller.postUrl('https://www.example.com', postData.buffer);
64          } catch (error) {
65            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
66          }
67        })
68      Web({ src: 'https://www.example.com', controller: this.controller })
69        .onControllerAttached(() => {
70          try {
71            this.schemeHandler.onRequestStart((request: webview.WebSchemeHandlerRequest, resourceHandler: webview.WebResourceHandler) => {
72              console.log("[schemeHandler] onRequestStart");
73              try {
74                let stream = request.getHttpBodyStream();
75                if (stream) {
76                  stream.initialize().then(() => {
77                    if (!stream) {
78                      return;
79                    }
80                    console.log("[schemeHandler] onRequestStart postDataStream size:" + stream.getSize());
81                    console.log("[schemeHandler] onRequestStart postDataStream position:" + stream.getPosition());
82                    console.log("[schemeHandler] onRequestStart postDataStream isChunked:" + stream.isChunked());
83                    console.log("[schemeHandler] onRequestStart postDataStream isEof:" + stream.isEof());
84                    console.log("[schemeHandler] onRequestStart postDataStream isInMemory:" + stream.isInMemory());
85                    stream.read(stream.getSize()).then((buffer) => {
86                      if (!stream) {
87                        return;
88                      }
89                      console.log("[schemeHandler] onRequestStart postDataStream readlength:" + buffer.byteLength);
90                      console.log("[schemeHandler] onRequestStart postDataStream isEof:" + stream.isEof());
91                      console.log("[schemeHandler] onRequestStart postDataStream position:" + stream.getPosition());
92                    }).catch((error: BusinessError) => {
93                      console.error(`ErrorCode: ${error.code},  Message: ${error.message}`);
94                    })
95                  }).catch((error: BusinessError) => {
96                    console.error(`ErrorCode: ${error.code},  Message: ${error.message}`);
97                  })
98                } else {
99                  console.log("[schemeHandler] onRequestStart has no http body stream");
100                }
101              } catch (error) {
102                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
103              }
104
105              return false;
106            })
107
108            this.schemeHandler.onRequestStop((request: webview.WebSchemeHandlerRequest) => {
109              console.log("[schemeHandler] onRequestStop");
110            });
111
112            this.controller.setWebSchemeHandler('https', this.schemeHandler);
113          } catch (error) {
114            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
115          }
116        })
117        .javaScriptAccess(true)
118        .domStorageAccess(true)
119    }
120  }
121}
122
123```
124
125## read<sup>12+</sup>
126
127read(size: number): Promise\<ArrayBuffer\>
128
129Reads data from this **WebHttpBodyStream** instance.
130
131**System capability**: SystemCapability.Web.Webview.Core
132
133**Parameters**
134
135| Name  | Type   |  Mandatory | Description                      |
136| --------| ------- | ---- | ---------------------------|
137|  size | number | Yes  | Number of bytes obtained from the **WebHttpBodyStream** instance.|
138
139**Return value**
140
141| Type  | Description                     |
142| ------ | ------------------------- |
143| Promise\<ArrayBuffer\> | Promise used to return the result.|
144
145**Error codes**
146
147For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
148
149| Error Code| Error Message                             |
150| -------- | ------------------------------------- |
151|  401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.    |
152
153**Example**
154
155For the complete sample code, see [initialize](#initialize12).
156
157## getSize<sup>12+</sup>
158
159getSize(): number
160
161Obtains the size of data in this **WebHttpBodyStream** instance. This API always returns zero when chunked transfer is used.
162
163**System capability**: SystemCapability.Web.Webview.Core
164
165**Return value**
166
167| Type  | Description                     |
168| ------ | ------------------------- |
169| number | Size of data in the current **WebHttpBodyStream** instance.|
170
171**Example**
172
173For the complete sample code, see [initialize](#initialize12).
174
175## getPosition<sup>12+</sup>
176
177getPosition(): number
178
179Reads the current read position in this **WebHttpBodyStream** instance.
180
181**System capability**: SystemCapability.Web.Webview.Core
182
183**Return value**
184
185| Type  | Description                     |
186| ------ | ------------------------- |
187| number | Current read position in the **WebHttpBodyStream** instance.|
188
189**Example**
190
191For the complete sample code, see [initialize](#initialize12).
192
193## isChunked<sup>12+</sup>
194
195isChunked(): boolean
196
197Checks whether this **WebHttpBodyStream** instance is transmitted by chunk.
198
199**System capability**: SystemCapability.Web.Webview.Core
200
201**Return value**
202
203| Type  | Description                     |
204| ------ | ------------------------- |
205| boolean | Whether the **WebHttpBodyStream** instance is transmitted by chunk. The value **true** indicates that the **WebHttpBodyStream** instance is transmitted by chunk, and **false** indicates the opposite.|
206
207**Example**
208
209For the complete sample code, see [initialize](#initialize12).
210
211## isEof<sup>12+</sup>
212
213isEof(): boolean
214
215Checks whether all data in this **WebHttpBodyStream** instance has been read.
216
217**System capability**: SystemCapability.Web.Webview.Core
218
219**Return value**
220
221| Type  | Description                     |
222| ------ | ------------------------- |
223| boolean | Whether all data in the **WebHttpBodyStream** instance has been read.<br>This API returns **true** if all data in the **httpBodyStream** instance is read. It returns **false** before the first read attempt is made for the **WebHttpBodyStream** instance that uses chunked transfer.|
224
225**Example**
226
227For the complete sample code, see [initialize](#initialize12).
228
229## isInMemory<sup>12+</sup>
230
231isInMemory(): boolean
232
233Checks whether the uploaded data in this **WebHttpBodyStream** instance is in memory.
234
235**System capability**: SystemCapability.Web.Webview.Core
236
237**Return value**
238
239| Type  | Description                     |
240| ------ | ------------------------- |
241| boolean | Whether the uploaded data in the **WebHttpBodyStream** instance is stored in memory.<br>This API returns **true** if all the upload data in the **WebHttpBodyStream** instance is in memory and all read requests will be completed synchronously. **false** is returned if the data is chunked.|
242
243**Example**
244
245For the complete sample code, see [initialize](#initialize12).
246