1# @ohos.net.http (Data Request) 2 3The **http** module provides the HTTP data request capability. An application can initiate a data request over HTTP. Common HTTP methods include **GET**, **POST**, **OPTIONS**, **HEAD**, **PUT**, **DELETE**, **TRACE**, and **CONNECT**. 4 5> **NOTE** 6> 7>The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9 10## Modules to Import 11 12```ts 13import http from '@ohos.net.http'; 14``` 15 16## Examples 17 18```ts 19// Import the http namespace. 20import http from '@ohos.net.http'; 21import { BusinessError } from '@ohos.base'; 22 23// Each httpRequest corresponds to an HTTP request task and cannot be reused. 24let httpRequest = http.createHttp(); 25// This API is used to listen for the HTTP Response Header event, which is returned earlier than the result of the HTTP request. It is up to you whether to listen for HTTP Response Header events. 26// on('headerReceive', AsyncCallback) is replaced by on('headersReceive', Callback) since API version 8. 27httpRequest.on('headersReceive', (header:Object) => { 28 console.info('header: ' + JSON.stringify(header)); 29}); 30class ExtraData { 31 public data: string; 32 33 constructor(data: string) { 34 this.data = data; 35 } 36} 37class Header { 38 public contentType: string; 39 40 constructor(contentType: string) { 41 this.contentType = contentType; 42 } 43} 44httpRequest.request( 45 // Customize EXAMPLE_URL in extraData on your own. It is up to you whether to add parameters to the URL. 46 "EXAMPLE_URL", 47 { 48 method: http.RequestMethod.POST, // Optional. The default value is http.RequestMethod.GET. 49 // You can add header fields based on service requirements. 50 header: new Header('application/json'), 51 // This parameter is used to transfer data when the POST request is used. 52 extraData: new ExtraData('data to send'), 53 expectDataType: http.HttpDataType.STRING, // Optional. This parameter specifies the type of the return data. 54 usingCache: true, // Optional. The default value is true. 55 priority: 1, // Optional. The default value is 1. 56 connectTimeout: 60000 // Optional. The default value is 60000, in ms. 57 readTimeout: 60000, // Optional. The default value is 60000, in ms. 58 usingProtocol: http.HttpProtocol.HTTP1_1, // Optional. The default protocol type is automatically specified by the system. 59 usingProxy: false, // Optional. By default, network proxy is not used. This field is supported since API version 10. 60 caPath: "", // Optional. The preset CA certificate is used by default. This field is supported since API version 10. 61 }, 62 (err: BusinessError, data: http.HttpResponse ) => { 63 if (!err) { 64 // data.result carries the HTTP response. Parse the response based on service requirements. 65 console.info('Result:' + JSON.stringify(data.result)); 66 console.info('code:' + JSON.stringify(data.responseCode)); 67 // data.header carries the HTTP response header. Parse the content based on service requirements. 68 console.info('header:' + JSON.stringify(data.header)); 69 console.info('cookies:' + JSON.stringify(data.cookies)); // 8+ 70 // Unsubscribe from HTTP Response Header events. 71 httpRequest.off('headersReceive'); 72 // Call destroy() to destroy the JavaScript object after the HTTP request is complete. 73 httpRequest.destroy(); 74 } else { 75 console.info('error:' + JSON.stringify(err)); 76 // Unsubscribe from HTTP Response Header events. 77 httpRequest.off('headersReceive'); 78 // Call destroy() to destroy the JavaScript object after the HTTP request is complete. 79 httpRequest.destroy(); 80 } 81); 82``` 83 84> **NOTE** 85> If the data in **console.info()** contains a newline character, the data will be truncated. 86 87## http.createHttp<sup>6+</sup> 88 89createHttp(): HttpRequest 90 91Creates an HTTP request. You can use this API to initiate or destroy an HTTP request, or enable or disable listening for HTTP Response Header events. An **HttpRequest** object corresponds to an HTTP request. To initiate multiple HTTP requests, you must create an **HttpRequest** object for each HTTP request. 92 93> **NOTE** 94> Call the **destroy()** method to release resources after the HttpRequest is complete. 95 96**System capability**: SystemCapability.Communication.NetStack 97 98**Return value** 99 100| Type | Description | 101| :---------- | :----------------------------------------------------------- | 102| HttpRequest | **HttpRequest** object, which contains the **request**, **requestInStream**, **destroy**, **on**, or **off** method.| 103 104**Example** 105 106```ts 107import http from '@ohos.net.http'; 108 109let httpRequest = http.createHttp(); 110``` 111 112## HttpRequest 113 114Defines an HTTP request task. Before invoking APIs provided by **HttpRequest**, you must call [createHttp()](#httpcreatehttp) to create an **HttpRequestTask** object. 115 116### request<sup>6+</sup> 117 118request(url: string, callback: AsyncCallback\<HttpResponse\>):void 119 120Initiates an HTTP request to a given URL. This API uses an asynchronous callback to return the result. 121 122> **NOTE** 123> This API supports only receiving of data not greater than 5 MB. 124 125**Required permissions**: ohos.permission.INTERNET 126 127**System capability**: SystemCapability.Communication.NetStack 128 129**Parameters** 130 131| Name | Type | Mandatory| Description | 132| -------- | ---------------------------------------------- | ---- | ----------------------- | 133| url | string | Yes | URL for initiating an HTTP request.| 134| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | Yes | Callback used to return the result. | 135 136**Error codes** 137 138| ID | Error Message | 139|---------|-------------------------------------------------------| 140| 401 | Parameter error. | 141| 201 | Permission denied. | 142| 2300001 | Unsupported protocol. | 143| 2300003 | URL using bad/illegal format or missing URL. | 144| 2300005 | Couldn't resolve proxy name. | 145| 2300006 | Couldn't resolve host name. | 146| 2300007 | Couldn't connect to server. | 147| 2300008 | Weird server reply. | 148| 2300009 | Access denied to remote resource. | 149| 2300016 | Error in the HTTP2 framing layer. | 150| 2300018 | Transferred a partial file. | 151| 2300023 | Failed writing received data to disk/application. | 152| 2300025 | Upload failed. | 153| 2300026 | Failed to open/read local data from file/application. | 154| 2300027 | Out of memory. | 155| 2300028 | Timeout was reached. | 156| 2300047 | Number of redirects hit maximum amount. | 157| 2300052 | Server returned nothing (no headers, no data). | 158| 2300055 | Failed sending data to the peer. | 159| 2300056 | Failure when receiving data from the peer. | 160| 2300058 | Problem with the local SSL certificate. | 161| 2300059 | Couldn't use specified SSL cipher. | 162| 2300060 | SSL peer certificate or SSH remote key was not OK. | 163| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 164| 2300063 | Maximum file size exceeded. | 165| 2300070 | Disk full or allocation exceeded. | 166| 2300073 | Remote file already exists. | 167| 2300077 | Problem with the SSL CA cert (path? access rights?). | 168| 2300078 | Remote file not found. | 169| 2300094 | An authentication function returned an error. | 170| 2300999 | Unknown Other Error. | 171 172> **NOTE** 173> For details about the error codes, see [HTTP Error Codes](../errorcodes/errorcode-net-http.md). 174> The HTTP error code mapping is in the format of 2300000 + Curl error code. For more common error codes, see [Curl Error Codes](https://curl.se/libcurl/c/libcurl-errors.html). 175 176**Example** 177 178```ts 179import http from '@ohos.net.http'; 180 181let httpRequest = http.createHttp(); 182httpRequest.request("EXAMPLE_URL", (err: Error, data: http.HttpResponse) => { 183 if (!err) { 184 console.info('Result:' + data.result); 185 console.info('code:' + data.responseCode); 186 console.info('header:' + JSON.stringify(data.header)); 187 console.info('cookies:' + data.cookies); // 8+ 188 } else { 189 console.info('error:' + JSON.stringify(err)); 190 } 191}); 192``` 193 194### request<sup>6+</sup> 195 196request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpResponse\>):void 197 198Initiates an HTTP request containing specified options to a given URL. This API uses an asynchronous callback to return the result. 199 200> **NOTE** 201> This API supports only receiving of data not greater than 5 MB. 202 203**Required permissions**: ohos.permission.INTERNET 204 205**System capability**: SystemCapability.Communication.NetStack 206 207**Parameters** 208 209| Name | Type | Mandatory| Description | 210| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 211| url | string | Yes | URL for initiating an HTTP request. | 212| options | HttpRequestOptions | Yes | Request options. For details, see [HttpRequestOptions](#httprequestoptions).| 213| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | Yes | Callback used to return the result. | 214 215**Error codes** 216 217| ID | Error Message | 218|---------|-------------------------------------------------------| 219| 401 | Parameter error. | 220| 201 | Permission denied. | 221| 2300001 | Unsupported protocol. | 222| 2300003 | URL using bad/illegal format or missing URL. | 223| 2300005 | Couldn't resolve proxy name. | 224| 2300006 | Couldn't resolve host name. | 225| 2300007 | Couldn't connect to server. | 226| 2300008 | Weird server reply. | 227| 2300009 | Access denied to remote resource. | 228| 2300016 | Error in the HTTP2 framing layer. | 229| 2300018 | Transferred a partial file. | 230| 2300023 | Failed writing received data to disk/application. | 231| 2300025 | Upload failed. | 232| 2300026 | Failed to open/read local data from file/application. | 233| 2300027 | Out of memory. | 234| 2300028 | Timeout was reached. | 235| 2300047 | Number of redirects hit maximum amount. | 236| 2300052 | Server returned nothing (no headers, no data). | 237| 2300055 | Failed sending data to the peer. | 238| 2300056 | Failure when receiving data from the peer. | 239| 2300058 | Problem with the local SSL certificate. | 240| 2300059 | Couldn't use specified SSL cipher. | 241| 2300060 | SSL peer certificate or SSH remote key was not OK. | 242| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 243| 2300063 | Maximum file size exceeded. | 244| 2300070 | Disk full or allocation exceeded. | 245| 2300073 | Remote file already exists. | 246| 2300077 | Problem with the SSL CA cert (path? access rights?). | 247| 2300078 | Remote file not found. | 248| 2300094 | An authentication function returned an error. | 249| 2300999 | Unknown Other Error. | 250 251> **NOTE** 252> For details about the error codes, see [HTTP Error Codes](../errorcodes/errorcode-net-http.md). 253> The HTTP error code mapping is in the format of 2300000 + Curl error code. For more common error codes, see [Curl Error Codes](https://curl.se/libcurl/c/libcurl-errors.html). 254 255**Example** 256 257```ts 258import http from '@ohos.net.http'; 259 260class Header { 261 public contentType: string; 262 263 constructor(contentType: string) { 264 this.contentType = contentType; 265 } 266} 267 268let httpRequest = http.createHttp(); 269let promise = httpRequest.request("EXAMPLE_URL", { 270 method: http.RequestMethod.GET, 271 connectTimeout: 60000, 272 readTimeout: 60000, 273 header: new Header('application/json') 274}); 275 276promise.then((data:http.HttpResponse) => { 277 console.info('Result:' + data.result); 278 console.info('code:' + data.responseCode); 279 console.info('header:' + JSON.stringify(data.header)); 280 console.info('cookies:' + data.cookies); // 8+ 281 console.info('header.Content-Type:' + data.header); 282 console.info('header.Status-Line:' + data.header); 283 284}).catch((err:Error) => { 285 console.info('error:' + JSON.stringify(err)); 286}); 287``` 288 289### request<sup>6+</sup> 290 291request(url: string, options? : HttpRequestOptions): Promise\<HttpResponse\> 292 293Initiates an HTTP request containing specified options to a given URL. This API uses a promise to return the result. 294 295> **NOTE** 296> This API supports only receiving of data not greater than 5 MB. 297 298**Required permissions**: ohos.permission.INTERNET 299 300**System capability**: SystemCapability.Communication.NetStack 301 302**Parameters** 303 304| Name | Type | Mandatory| Description | 305| ------- | ------------------ | ---- | ----------------------------------------------- | 306| url | string | Yes | URL for initiating an HTTP request. | 307| options | HttpRequestOptions | No | Request options. For details, see [HttpRequestOptions](#httprequestoptions).| 308 309**Return value** 310 311| Type | Description | 312| :------------------------------------- | :-------------------------------- | 313| Promise<[HttpResponse](#httpresponse)> | Promise used to return the result.| 314 315**Error codes** 316 317| ID | Error Message | 318|---------|-------------------------------------------------------| 319| 401 | Parameter error. | 320| 201 | Permission denied. | 321| 2300001 | Unsupported protocol. | 322| 2300003 | URL using bad/illegal format or missing URL. | 323| 2300005 | Couldn't resolve proxy name. | 324| 2300006 | Couldn't resolve host name. | 325| 2300007 | Couldn't connect to server. | 326| 2300008 | Weird server reply. | 327| 2300009 | Access denied to remote resource. | 328| 2300016 | Error in the HTTP2 framing layer. | 329| 2300018 | Transferred a partial file. | 330| 2300023 | Failed writing received data to disk/application. | 331| 2300025 | Upload failed. | 332| 2300026 | Failed to open/read local data from file/application. | 333| 2300027 | Out of memory. | 334| 2300028 | Timeout was reached. | 335| 2300047 | Number of redirects hit maximum amount. | 336| 2300052 | Server returned nothing (no headers, no data). | 337| 2300055 | Failed sending data to the peer. | 338| 2300056 | Failure when receiving data from the peer. | 339| 2300058 | Problem with the local SSL certificate. | 340| 2300059 | Couldn't use specified SSL cipher. | 341| 2300060 | SSL peer certificate or SSH remote key was not OK. | 342| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 343| 2300063 | Maximum file size exceeded. | 344| 2300070 | Disk full or allocation exceeded. | 345| 2300073 | Remote file already exists. | 346| 2300077 | Problem with the SSL CA cert (path? access rights?). | 347| 2300078 | Remote file not found. | 348| 2300094 | An authentication function returned an error. | 349| 2300999 | Unknown Other Error. | 350 351> **NOTE** 352> For details about the error codes, see [HTTP Error Codes](../errorcodes/errorcode-net-http.md). 353> The HTTP error code mapping is in the format of 2300000 + Curl error code. For more common error codes, see [Curl Error Codes](https://curl.se/libcurl/c/libcurl-errors.html). 354 355**Example** 356 357```ts 358import http from '@ohos.net.http'; 359 360class Header { 361 public contentType: string; 362 363 constructor(contentType: string) { 364 this.contentType = contentType; 365 } 366} 367 368let httpRequest = http.createHttp(); 369let promise = httpRequest.request("EXAMPLE_URL", { 370 method: http.RequestMethod.GET, 371 connectTimeout: 60000, 372 readTimeout: 60000, 373 header: new Header('application/json') 374}); 375promise.then((data:http.HttpResponse) => { 376 console.info('Result:' + data.result); 377 console.info('code:' + data.responseCode); 378 console.info('header:' + JSON.stringify(data.header)); 379 console.info('cookies:' + data.cookies); // 8+ 380 console.info('header.Content-Type:' + data.header); 381 console.info('header.Status-Line:' + data.header); 382}).catch((err:Error) => { 383 console.info('error:' + JSON.stringify(err)); 384}); 385``` 386 387### destroy 388 389destroy(): void 390 391Destroys an HTTP request. 392 393**System capability**: SystemCapability.Communication.NetStack 394 395**Example** 396 397```ts 398import http from '@ohos.net.http'; 399let httpRequest = http.createHttp(); 400 401httpRequest.destroy(); 402``` 403 404### requestInStream<sup>10+</sup> 405 406requestInStream(url: string, callback: AsyncCallback\<number\>): void 407 408Initiates an HTTP request containing specified options to a given URL. This API uses an asynchronous callback to return the result, which is a streaming response. 409 410**Required permissions**: ohos.permission.INTERNET 411 412**System capability**: SystemCapability.Communication.NetStack 413 414**Parameters** 415 416| Name | Type | Mandatory| Description | 417| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 418| url | string | Yes | URL for initiating an HTTP request. | 419| callback | AsyncCallback\<[number](#responsecode)\> | Yes | Callback used to return the result. | 420 421**Error codes** 422 423| ID | Error Message | 424|---------|-------------------------------------------------------| 425| 401 | Parameter error. | 426| 201 | Permission denied. | 427| 2300001 | Unsupported protocol. | 428| 2300003 | URL using bad/illegal format or missing URL. | 429| 2300005 | Couldn't resolve proxy name. | 430| 2300006 | Couldn't resolve host name. | 431| 2300007 | Couldn't connect to server. | 432| 2300008 | Weird server reply. | 433| 2300009 | Access denied to remote resource. | 434| 2300016 | Error in the HTTP2 framing layer. | 435| 2300018 | Transferred a partial file. | 436| 2300023 | Failed writing received data to disk/application. | 437| 2300025 | Upload failed. | 438| 2300026 | Failed to open/read local data from file/application. | 439| 2300027 | Out of memory. | 440| 2300028 | Timeout was reached. | 441| 2300047 | Number of redirects hit maximum amount. | 442| 2300052 | Server returned nothing (no headers, no data). | 443| 2300055 | Failed sending data to the peer. | 444| 2300056 | Failure when receiving data from the peer. | 445| 2300058 | Problem with the local SSL certificate. | 446| 2300059 | Couldn't use specified SSL cipher. | 447| 2300060 | SSL peer certificate or SSH remote key was not OK. | 448| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 449| 2300063 | Maximum file size exceeded. | 450| 2300070 | Disk full or allocation exceeded. | 451| 2300073 | Remote file already exists. | 452| 2300077 | Problem with the SSL CA cert (path? access rights?). | 453| 2300078 | Remote file not found. | 454| 2300094 | An authentication function returned an error. | 455| 2300999 | Unknown Other Error. | 456 457> **NOTE** 458> For details about the error codes, see [HTTP Error Codes](../errorcodes/errorcode-net-http.md). 459> The HTTP error code mapping is in the format of 2300000 + Curl error code. For more common error codes, see [Curl Error Codes](https://curl.se/libcurl/c/libcurl-errors.html). 460 461**Example** 462 463```ts 464import http from '@ohos.net.http'; 465import { BusinessError } from '@ohos.base'; 466 467let httpRequest = http.createHttp(); 468httpRequest.requestInStream("EXAMPLE_URL", (err: BusinessError, data: number) => { 469 if (!err) { 470 console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data)); 471 } else { 472 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 473 } 474}) 475``` 476 477### requestInStream<sup>10+</sup> 478 479requestInStream(url: string, options: HttpRequestOptions, callback: AsyncCallback\<number\>): void 480 481Initiates an HTTP request containing specified options to a given URL. This API uses an asynchronous callback to return the result, which is a streaming response. 482 483**Required permissions**: ohos.permission.INTERNET 484 485**System capability**: SystemCapability.Communication.NetStack 486 487**Parameters** 488 489| Name | Type | Mandatory| Description | 490| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 491| url | string | Yes | URL for initiating an HTTP request. | 492| options | HttpRequestOptions | Yes | Request options. For details, see [HttpRequestOptions](#httprequestoptions).| 493| callback | AsyncCallback\<[number](#responsecode)\> | Yes | Callback used to return the result. | 494 495**Error codes** 496 497| ID | Error Message | 498|---------|-------------------------------------------------------| 499| 401 | Parameter error. | 500| 201 | Permission denied. | 501| 2300001 | Unsupported protocol. | 502| 2300003 | URL using bad/illegal format or missing URL. | 503| 2300005 | Couldn't resolve proxy name. | 504| 2300006 | Couldn't resolve host name. | 505| 2300007 | Couldn't connect to server. | 506| 2300008 | Weird server reply. | 507| 2300009 | Access denied to remote resource. | 508| 2300016 | Error in the HTTP2 framing layer. | 509| 2300018 | Transferred a partial file. | 510| 2300023 | Failed writing received data to disk/application. | 511| 2300025 | Upload failed. | 512| 2300026 | Failed to open/read local data from file/application. | 513| 2300027 | Out of memory. | 514| 2300028 | Timeout was reached. | 515| 2300047 | Number of redirects hit maximum amount. | 516| 2300052 | Server returned nothing (no headers, no data). | 517| 2300055 | Failed sending data to the peer. | 518| 2300056 | Failure when receiving data from the peer. | 519| 2300058 | Problem with the local SSL certificate. | 520| 2300059 | Couldn't use specified SSL cipher. | 521| 2300060 | SSL peer certificate or SSH remote key was not OK. | 522| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 523| 2300063 | Maximum file size exceeded. | 524| 2300070 | Disk full or allocation exceeded. | 525| 2300073 | Remote file already exists. | 526| 2300077 | Problem with the SSL CA cert (path? access rights?). | 527| 2300078 | Remote file not found. | 528| 2300094 | An authentication function returned an error. | 529| 2300999 | Unknown Other Error. | 530 531> **NOTE** 532> For details about the error codes, see [HTTP Error Codes](../errorcodes/errorcode-net-http.md). 533> The HTTP error code mapping is in the format of 2300000 + Curl error code. For more common error codes, see [Curl Error Codes](https://curl.se/libcurl/c/libcurl-errors.html). 534 535**Example** 536 537```ts 538import http from '@ohos.net.http'; 539import { BusinessError } from '@ohos.base'; 540 541class Header { 542 public contentType: string; 543 544 constructor(contentType: string) { 545 this.contentType = contentType; 546 } 547} 548 549let httpRequest = http.createHttp(); 550httpRequest.requestInStream("EXAMPLE_URL", (err: BusinessError<void> , data: number) => { 551 if (!err) { 552 console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data)); 553 } else { 554 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 555 } 556}) 557``` 558 559### requestInStream<sup>10+</sup> 560 561requestInStream(url: string, options? : HttpRequestOptions): Promise\<number\> 562 563Initiates an HTTP request containing specified options to a given URL. This API uses a promise to return the result, which is a streaming response. 564 565**Required permissions**: ohos.permission.INTERNET 566 567**System capability**: SystemCapability.Communication.NetStack 568 569**Parameters** 570 571| Name | Type | Mandatory| Description | 572| ------- | ------------------ | ---- | ----------------------------------------------- | 573| url | string | Yes | URL for initiating an HTTP request. | 574| options | HttpRequestOptions | No | Request options. For details, see [HttpRequestOptions](#httprequestoptions).| 575 576**Return value** 577 578| Type | Description | 579| :------------------------------------- | :-------------------------------- | 580| Promise\<[number](#responsecode)\> | Promise used to return the result.| 581 582**Error codes** 583 584| ID | Error Message | 585|---------|-------------------------------------------------------| 586| 401 | Parameter error. | 587| 201 | Permission denied. | 588| 2300001 | Unsupported protocol. | 589| 2300003 | URL using bad/illegal format or missing URL. | 590| 2300005 | Couldn't resolve proxy name. | 591| 2300006 | Couldn't resolve host name. | 592| 2300007 | Couldn't connect to server. | 593| 2300008 | Weird server reply. | 594| 2300009 | Access denied to remote resource. | 595| 2300016 | Error in the HTTP2 framing layer. | 596| 2300018 | Transferred a partial file. | 597| 2300023 | Failed writing received data to disk/application. | 598| 2300025 | Upload failed. | 599| 2300026 | Failed to open/read local data from file/application. | 600| 2300027 | Out of memory. | 601| 2300028 | Timeout was reached. | 602| 2300047 | Number of redirects hit maximum amount. | 603| 2300052 | Server returned nothing (no headers, no data). | 604| 2300055 | Failed sending data to the peer. | 605| 2300056 | Failure when receiving data from the peer. | 606| 2300058 | Problem with the local SSL certificate. | 607| 2300059 | Couldn't use specified SSL cipher. | 608| 2300060 | SSL peer certificate or SSH remote key was not OK. | 609| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 610| 2300063 | Maximum file size exceeded. | 611| 2300070 | Disk full or allocation exceeded. | 612| 2300073 | Remote file already exists. | 613| 2300077 | Problem with the SSL CA cert (path? access rights?). | 614| 2300078 | Remote file not found. | 615| 2300094 | An authentication function returned an error. | 616| 2300999 | Unknown Other Error. | 617 618> **NOTE** 619> For details about the error codes, see [HTTP Error Codes](../errorcodes/errorcode-net-http.md). 620> The HTTP error code mapping is in the format of 2300000 + Curl error code. For more common error codes, see [Curl Error Codes](https://curl.se/libcurl/c/libcurl-errors.html). 621 622**Example** 623 624```ts 625import http from '@ohos.net.http'; 626 627class Header { 628 public contentType: string; 629 630 constructor(contentType: string) { 631 this.contentType = contentType; 632 } 633} 634 635let httpRequest = http.createHttp(); 636let promise = httpRequest.request("EXAMPLE_URL", { 637 method: http.RequestMethod.GET, 638 connectTimeout: 60000, 639 readTimeout: 60000, 640 header: new Header('application/json') 641}); 642promise.then((data: http.HttpResponse) => { 643 console.info("requestInStream OK!" + JSON.stringify(data)); 644}).catch((err: Error) => { 645 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 646}); 647``` 648 649### on('headerReceive')<sup>(deprecated)</sup> 650 651on(type: 'headerReceive', callback: AsyncCallback\<Object\>): void 652 653Registers an observer for HTTP Response Header events. 654 655> **NOTE** 656> This API has been deprecated. You are advised to use [on('headersReceive')<sup>8+</sup>](#onheadersreceive8). 657 658**System capability**: SystemCapability.Communication.NetStack 659 660**Parameters** 661 662| Name | Type | Mandatory| Description | 663| -------- | ----------------------- | ---- | --------------------------------- | 664| type | string | Yes | Event type. The value is **headerReceive**.| 665| callback | AsyncCallback\<Object\> | Yes | Callback used to return the result. | 666 667**Example** 668 669```ts 670import http from '@ohos.net.http'; 671import { BusinessError } from '@ohos.base'; 672 673let httpRequest = http.createHttp(); 674httpRequest.on('headerReceive', (data: BusinessError) => { 675 console.info('error:' + JSON.stringify(data)); 676}); 677``` 678 679### off('headerReceive')<sup>(deprecated)</sup> 680 681off(type: 'headerReceive', callback?: AsyncCallback\<Object\>): void 682 683Unregisters the observer for HTTP Response Header events. 684 685> **NOTE** 686> 687>1. This API has been deprecated. You are advised to use [off('headersReceive')<sup>8+</sup>](#offheadersreceive8). 688> 689>2. You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 690 691**System capability**: SystemCapability.Communication.NetStack 692 693**Parameters** 694 695| Name | Type | Mandatory| Description | 696| -------- | ----------------------- | ---- | ------------------------------------- | 697| type | string | Yes | Event type. The value is **headerReceive**.| 698| callback | AsyncCallback\<Object\> | No | Callback used to return the result. | 699 700**Example** 701 702```ts 703import http from '@ohos.net.http'; 704 705let httpRequest = http.createHttp(); 706httpRequest.off('headerReceive'); 707``` 708 709### on('headersReceive')<sup>8+</sup> 710 711on(type: 'headersReceive', callback: Callback\<Object\>): void 712 713Registers an observer for HTTP Response Header events. 714 715**System capability**: SystemCapability.Communication.NetStack 716 717**Parameters** 718 719| Name | Type | Mandatory| Description | 720| -------- | ------------------ | ---- | ---------------------------------- | 721| type | string | Yes | Event type. The value is **headersReceive**.| 722| callback | Callback\<Object\> | Yes | Callback used to return the result. | 723 724**Example** 725 726```ts 727import http from '@ohos.net.http'; 728 729let httpRequest = http.createHttp(); 730httpRequest.on('headersReceive', (header: Object) => { 731 console.info('header: ' + JSON.stringify(header)); 732}); 733``` 734 735### off('headersReceive')<sup>8+</sup> 736 737off(type: 'headersReceive', callback?: Callback\<Object\>): void 738 739Unregisters the observer for HTTP Response Header events. 740 741> **NOTE** 742> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 743 744**System capability**: SystemCapability.Communication.NetStack 745 746**Parameters** 747 748| Name | Type | Mandatory| Description | 749| -------- | ------------------ | ---- | -------------------------------------- | 750| type | string | Yes | Event type. The value is **headersReceive**.| 751| callback | Callback\<Object\> | No | Callback used to return the result. | 752 753**Example** 754 755```ts 756import http from '@ohos.net.http'; 757 758let httpRequest = http.createHttp(); 759httpRequest.off('headersReceive'); 760``` 761 762### once('headersReceive')<sup>8+</sup> 763 764once(type: 'headersReceive', callback: Callback\<Object\>): void 765 766Registers a one-time observer for HTTP Response Header events. Once triggered, the observer will be removed. This API uses an asynchronous callback to return the result. 767 768**System capability**: SystemCapability.Communication.NetStack 769 770**Parameters** 771 772| Name | Type | Mandatory| Description | 773| -------- | ------------------ | ---- | ---------------------------------- | 774| type | string | Yes | Event type. The value is **headersReceive**.| 775| callback | Callback\<Object\> | Yes | Callback used to return the result. | 776 777**Example** 778 779```ts 780import http from '@ohos.net.http'; 781 782let httpRequest = http.createHttp(); 783httpRequest.once('headersReceive', (header: Object) => { 784 console.info('header: ' + JSON.stringify(header)); 785}); 786``` 787 788### on('dataReceive')<sup>10+</sup> 789 790on(type: 'dataReceive', callback: Callback\<ArrayBuffer\>): void 791 792Registers an observer for events indicating receiving of HTTP streaming responses. 793 794> **NOTE** 795> Currently, listening for events related to HTTP streaming data upload is not supported. 796 797**System capability**: SystemCapability.Communication.NetStack 798 799**Parameters** 800 801| Name | Type | Mandatory| Description | 802| -------- | ----------------------- | ---- | --------------------------------- | 803| type | string | Yes | Event type. The value is **dataReceive**.| 804| callback | AsyncCallback\<ArrayBuffer\> | Yes | Callback used to return the result. | 805 806**Example** 807 808```ts 809import http from '@ohos.net.http'; 810 811let httpRequest = http.createHttp(); 812httpRequest.on('dataReceive', (data: ArrayBuffer) => { 813 console.info('dataReceive length: ' + JSON.stringify(data.byteLength)); 814}); 815``` 816 817### off('dataReceive')<sup>10+</sup> 818 819off(type: 'dataReceive', callback?: Callback\<ArrayBuffer\>): void 820 821Unregisters the observer for events indicating receiving of HTTP streaming responses. 822 823> **NOTE** 824> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 825 826**System capability**: SystemCapability.Communication.NetStack 827 828**Parameters** 829 830| Name | Type | Mandatory| Description | 831| -------- | ------------------ | ---- | -------------------------------------- | 832| type | string | Yes | Event type. The value is **dataReceive**.| 833| callback | Callback\<ArrayBuffer\> | No | Callback used to return the result. | 834 835**Example** 836 837```ts 838import http from '@ohos.net.http'; 839 840let httpRequest = http.createHttp(); 841httpRequest.off('dataReceive'); 842``` 843 844### on('dataEnd')<sup>10+</sup> 845 846on(type: 'dataEnd', callback: Callback\<void\>): void 847 848Registers an observer for events indicating completion of receiving HTTP streaming responses. 849 850> **NOTE** 851> Currently, listening for events related to HTTP streaming data upload is not supported. 852 853**System capability**: SystemCapability.Communication.NetStack 854 855**Parameters** 856 857| Name | Type | Mandatory| Description | 858| -------- | ----------------------- | ---- | --------------------------------- | 859| type | string | Yes | Event type. The value is **dataEnd**.| 860| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. | 861 862**Example** 863 864```ts 865import http from '@ohos.net.http'; 866 867let httpRequest = http.createHttp(); 868httpRequest.on('dataEnd', () => { 869 console.info('Receive dataEnd !'); 870}); 871``` 872 873### off('dataEnd')<sup>10+</sup> 874 875off(type: 'dataEnd', callback?: Callback\<void\>): void 876 877Unregisters the observer for events indicating completion of receiving HTTP streaming responses. 878 879> **NOTE** 880> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 881 882**System capability**: SystemCapability.Communication.NetStack 883 884**Parameters** 885 886| Name | Type | Mandatory| Description | 887| -------- | ------------------ | ---- | -------------------------------------- | 888| type | string | Yes | Event type. The value is **dataEnd**.| 889| callback | Callback\<void\> | No | Callback used to return the result. | 890 891**Example** 892 893```ts 894import http from '@ohos.net.http'; 895 896let httpRequest = http.createHttp(); 897httpRequest.off('dataEnd'); 898``` 899 900### on('dataReceiveProgress')<sup>10+</sup> 901 902on(type: 'dataReceiveProgress', callback: Callback\<{ receiveSize: number, totalSize: number }\>): void 903 904Registers an observer for events indicating progress of receiving HTTP streaming responses. 905 906> **NOTE** 907> Currently, listening for events related to HTTP streaming data upload is not supported. 908 909**System capability**: SystemCapability.Communication.NetStack 910 911**Parameters** 912 913| Name | Type | Mandatory| Description | 914| -------- | ----------------------- | ---- | --------------------------------- | 915| type | string | Yes | Event type. The value is **dataReceiveProgress**.| 916| callback | AsyncCallback\<{ receiveSize: number, totalSize: number }\> | Yes | Callback used to return the result.<br>- **receiveSize**: number of received bytes.<br>- **totalSize**: total number of bytes to be received.| 917 918**Example** 919 920```ts 921import http from '@ohos.net.http'; 922 923class RequestData{ 924 receiveSize: number = 2000 925 totalSize: number = 2000 926} 927 928let httpRequest = http.createHttp(); 929httpRequest.on('dataReceiveProgress', (data: RequestData) => { 930 console.info('dataReceiveProgress:' + JSON.stringify(data)); 931}); 932``` 933 934### off('dataReceiveProgress')<sup>10+</sup> 935 936off(type: 'dataReceiveProgress', callback?: Callback\<{ receiveSize: number, totalSize: number }\>): void 937 938Unregisters the observer for events indicating progress of receiving HTTP streaming responses. 939 940> **NOTE** 941> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 942 943**System capability**: SystemCapability.Communication.NetStack 944 945**Parameters** 946 947| Name | Type | Mandatory| Description | 948| -------- | ------------------ | ---- | -------------------------------------- | 949| type | string | Yes | Event type. The value is **dataReceiveProgress**.| 950| callback | Callback\<{ receiveSize: number, totalSize: number }\> | No | Callback used to return the result. | 951 952**Example** 953 954```ts 955import http from '@ohos.net.http'; 956 957let httpRequest = http.createHttp(); 958httpRequest.off('dataReceiveProgress'); 959``` 960 961## HttpRequestOptions<sup>6+</sup> 962 963Specifies the type and value range of the optional parameters in the HTTP request. 964 965**System capability**: SystemCapability.Communication.NetStack 966 967| Name | Type | Mandatory| Description | 968| -------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 969| method | [RequestMethod](#requestmethod) | No | Request method. The default value is **GET**. | 970| extraData | string<sup>6+</sup> \| Object<sup>6+</sup> \| ArrayBuffer<sup>8+</sup> | No | Additional data for sending a request. This parameter is not used by default.<br>- If the HTTP request uses a POST or PUT method, this parameter serves as the content of the HTTP request and is encoded in UTF-8 format. If **Content-Type** is **application/x-www-form-urlencoded**, the data in the request body must be encoded in the format of **key1=value1&key2=value2&key3=value3** after URL transcoding and this field is usually in the String format. If **Content-Type** is **text/xml**, this field is usually in the String format. If **Content-Type** is **application/json**, this field is usually in the Object format. If **Content-Type** is **application/octet-stream**, this field is usually in the ArrayBuffer format. If **Content-Type** is **multipart/form-data** and the content to be uploaded is a file, this field is usually in the ArrayBuffer format. The preceding information is for reference only and may vary according to the actual situation.<br>- If the HTTP request uses the GET, OPTIONS, DELETE, TRACE, or CONNECT method, this parameter serves as a supplement to HTTP request parameters. Parameters of the string type need to be encoded before being passed to the HTTP request. Parameters of the object type do not need to be precoded and will be directly concatenated to the URL. Parameters of the ArrayBuffer type will not be concatenated to the URL.| 971| <span name="expectDataType">[expectDataType<sup>9+</sup>](#result)</span> | [HttpDataType](#httpdatatype9) | No | Type of the returned data. This parameter is not used by default. If this parameter is set, the system returns the specified type of data preferentially.| 972| usingCache<sup>9+</sup> | boolean | No | Whether to use the cache. The default value is **true**. | 973| priority<sup>9+</sup> | number | No | Priority. The value range is [1,1000]. The default value is **1**. | 974| header | Object | No | HTTP request header. The default value is **{'Content-Type': 'application/json'}**. | 975| readTimeout | number | No | Read timeout duration. The default value is **60000**, in ms.<br>The value **0** indicates no timeout.| 976| connectTimeout | number | No | Connection timeout interval. The default value is **60000**, in ms. | 977| usingProtocol<sup>9+</sup> | [HttpProtocol](#httpprotocol9) | No | Protocol. The default value is automatically specified by the system. | 978| usingProxy<sup>10+</sup> | boolean \| Object | No | Whether to use HTTP proxy. The default value is **false**, which means not to use HTTP proxy.<br>- If **usingProxy** is of the **Boolean** type and the value is **true**, network proxy is used by default.<br>- If **usingProxy** is of the **object** type, the specified network proxy is used. 979| caPath<sup>10+</sup> | string | No | Path of CA certificates. If a path is set, the system uses the CA certificates in this path. If a path is not set, the system uses the preset CA certificate, namely, **/etc/ssl/certs/cacert.pem**. This path is the sandbox mapping path, which can be obtained through **Global.getContext().filesDir**. Currently, only **.pem** certificates are supported. | 980 981## RequestMethod<sup>6+</sup> 982 983Defines an HTTP request method. 984 985**System capability**: SystemCapability.Communication.NetStack 986 987| Name | Value | Description | 988| :------ | ------- | :------------------ | 989| OPTIONS | "OPTIONS" | OPTIONS method.| 990| GET | "GET" | GET method. | 991| HEAD | "HEAD" | HEAD method. | 992| POST | "POST" | POST method. | 993| PUT | "PUT" | PUT method. | 994| DELETE | "DELETE" | DELETE method. | 995| TRACE | "TRACE" | TRACE method. | 996| CONNECT | "CONNECT" | CONNECT method.| 997 998## ResponseCode<sup>6+</sup> 999 1000Enumerates the response codes for an HTTP request. 1001 1002**System capability**: SystemCapability.Communication.NetStack 1003 1004| Name | Value | Description | 1005| ----------------- | ---- | ------------------------------------------------------------ | 1006| OK | 200 | The request is successful. The request has been processed successfully. This return code is generally used for GET and POST requests. | 1007| CREATED | 201 | "Created." The request has been successfully sent and a new resource is created. | 1008| ACCEPTED | 202 | "Accepted." The request has been accepted, but the processing has not been completed. | 1009| NOT_AUTHORITATIVE | 203 | "Non-Authoritative Information." The request is successful. | 1010| NO_CONTENT | 204 | "No Content." The server has successfully fulfilled the request but there is no additional content to send in the response payload body. | 1011| RESET | 205 | "Reset Content." The server has successfully fulfilled the request and desires that the user agent reset the content. | 1012| PARTIAL | 206 | "Partial Content." The server has successfully fulfilled the partial GET request for a given resource. | 1013| MULT_CHOICE | 300 | "Multiple Choices." The requested resource corresponds to any one of a set of representations. | 1014| MOVED_PERM | 301 | "Moved Permanently." The requested resource has been assigned a new permanent URI and any future references to this resource will be redirected to this URI.| 1015| MOVED_TEMP | 302 | "Moved Temporarily." The requested resource is moved temporarily to a different URI. | 1016| SEE_OTHER | 303 | "See Other." The response to the request can be found under a different URI. | 1017| NOT_MODIFIED | 304 | "Not Modified." The client has performed a conditional GET request and access is allowed, but the content has not been modified. | 1018| USE_PROXY | 305 | "Use Proxy." The requested resource can only be accessed through the proxy. | 1019| BAD_REQUEST | 400 | "Bad Request." The request could not be understood by the server due to incorrect syntax. | 1020| UNAUTHORIZED | 401 | "Unauthorized." The request requires user authentication. | 1021| PAYMENT_REQUIRED | 402 | "Payment Required." This code is reserved for future use. | 1022| FORBIDDEN | 403 | "Forbidden." The server understands the request but refuses to process it. | 1023| NOT_FOUND | 404 | "Not Found." The server does not find anything matching the Request-URI. | 1024| BAD_METHOD | 405 | "Method Not Allowed." The method specified in the request is not allowed for the resource identified by the Request-URI. | 1025| NOT_ACCEPTABLE | 406 | "Not Acceptable." The server cannot fulfill the request according to the content characteristics of the request. | 1026| PROXY_AUTH | 407 | "Proxy Authentication Required." The request requires user authentication with the proxy. | 1027| CLIENT_TIMEOUT | 408 | "Request Timeout." The client fails to generate a request within the timeout period. | 1028| CONFLICT | 409 | "Conflict." The request cannot be fulfilled due to a conflict with the current state of the resource. Conflicts are most likely to occur in response to a PUT request. | 1029| GONE | 410 | "Gone." The requested resource has been deleted permanently and is no longer available. | 1030| LENGTH_REQUIRED | 411 | "Length Required." The server refuses to process the request without a defined Content-Length. | 1031| PRECON_FAILED | 412 | "Precondition Failed." The precondition in the request is incorrect. | 1032| ENTITY_TOO_LARGE | 413 | "Request Entity Too Large." The server refuses to process a request because the request entity is larger than the server is able to process. | 1033| REQ_TOO_LONG | 414 | "Request-URI Too Long." The Request-URI is too long for the server to process. | 1034| UNSUPPORTED_TYPE | 415 | "Unsupported Media Type." The server is unable to process the media format in the request. | 1035| INTERNAL_ERROR | 500 | "Internal Server Error." The server encounters an unexpected error that prevents it from fulfilling the request. | 1036| NOT_IMPLEMENTED | 501 | "Not Implemented." The server does not support the function required to fulfill the request. | 1037| BAD_GATEWAY | 502 | "Bad Gateway." The server acting as a gateway or proxy receives an invalid response from the upstream server.| 1038| UNAVAILABLE | 503 | "Service Unavailable." The server is currently unable to process the request due to a temporary overload or system maintenance. | 1039| GATEWAY_TIMEOUT | 504 | "Gateway Timeout." The server acting as a gateway or proxy does not receive requests from the remote server within the timeout period. | 1040| VERSION | 505 | "HTTP Version Not Supported." The server does not support the HTTP protocol version used in the request. | 1041 1042## HttpResponse<sup>6+</sup> 1043 1044Defines the response to an HTTP request. 1045 1046**System capability**: SystemCapability.Communication.NetStack 1047 1048| Name | Type | Mandatory| Description | 1049| -------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 1050| <span name="result">[result](#expectDataType)</span> | string<sup>6+</sup> \| Object<sup>deprecated 8+</sup> \| ArrayBuffer<sup>8+</sup> | Yes | Response content returned based on **Content-type** in the response header. If **HttpRequestOptions** does not contain the **expectDataType** field, the response content is returned according to the following rules:<br>- application/json: string in JSON format<br>- application/octet-stream: ArrayBuffer<br>- image: ArrayBuffer<br>- Others: string<br> If **HttpRequestOptions** contains the **expectDataType** field, the response content must be of the same type as the data returned by the server.| 1051| resultType<sup>9+</sup> | [HttpDataType](#httpdatatype9) | Yes | Type of the return value. | 1052| responseCode | [ResponseCode](#responsecode) \| number | Yes | Result code for an HTTP request. If the callback function is successfully executed, a result code defined in [ResponseCode](#responsecode) will be returned. Otherwise, an error code will be returned in the **err** field in **AsyncCallback**.| 1053| header | Object | Yes | Response header. The return value is a string in JSON format. If you want to use specific content in the response, you need to implement parsing of that content. Common fields and parsing methods are as follows:<br>- content-type: header['content-type'];<br>- status-line: header['status-line'];<br>- date: header.date/header['date'];<br>- server: header.server/header['server'];| 1054| cookies<sup>8+</sup> | string | Yes | Cookies returned by the server. | 1055 1056## http.createHttpResponseCache<sup>9+</sup> 1057 1058createHttpResponseCache(cacheSize?: number): HttpResponseCache 1059 1060Creates a default object to store responses to HTTP access requests. 1061 1062**System capability**: SystemCapability.Communication.NetStack 1063 1064**Parameters** 1065 1066| Name | Type | Mandatory| Description | 1067| -------- | --------------------------------------- | ---- | ---------- | 1068| cacheSize | number | No| Cache size. The maximum value is 10\*1024\*1024 (10 MB). By default, the maximum value is used.| 1069 1070**Return value** 1071 1072| Type | Description | 1073| :---------- | :----------------------------------------------------------- | 1074| [HttpResponseCache](#httpresponsecache9) | Object that stores the response to the HTTP request.| 1075 1076**Example** 1077 1078```ts 1079import http from '@ohos.net.http'; 1080 1081let httpResponseCache = http.createHttpResponseCache(); 1082``` 1083 1084## HttpResponseCache<sup>9+</sup> 1085 1086Defines an object that stores the response to an HTTP request. Before invoking APIs provided by **HttpResponseCache**, you must call [createHttpResponseCache()](#httpcreatehttpresponsecache9) to create an **HttpRequestTask** object. 1087 1088### flush<sup>9+</sup> 1089 1090flush(callback: AsyncCallback\<void\>): void 1091 1092Flushes cached data to the file system so that the data can be accessed in the next HTTP request. This API uses an asynchronous callback to return the result. Cached data includes the response header (header), response body (result), cookies, request time (requestTime), and response time (responseTime). 1093 1094**System capability**: SystemCapability.Communication.NetStack 1095 1096**Parameters** 1097 1098| Name | Type | Mandatory| Description | 1099| -------- | --------------------------------------- | ---- | ---------- | 1100| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 1101 1102**Example** 1103 1104```ts 1105import http from '@ohos.net.http'; 1106import { BusinessError } from '@ohos.base'; 1107 1108let httpResponseCache = http.createHttpResponseCache(); 1109httpResponseCache.flush((err: BusinessError) => { 1110 if (err) { 1111 console.info('flush fail'); 1112 return; 1113 } 1114 console.info('flush success'); 1115}); 1116``` 1117 1118### flush<sup>9+</sup> 1119 1120flush(): Promise\<void\> 1121 1122Flushes cached data to the file system so that the data can be accessed in the next HTTP request. This API uses a promise to return the result. 1123 1124**System capability**: SystemCapability.Communication.NetStack 1125 1126**Return value** 1127 1128| Type | Description | 1129| --------------------------------- | ------------------------------------- | 1130| Promise\<void\> | Promise used to return the result.| 1131 1132**Example** 1133 1134```ts 1135import http from '@ohos.net.http'; 1136import { BusinessError } from '@ohos.base'; 1137 1138let httpResponseCache = http.createHttpResponseCache(); 1139httpResponseCache.flush().then(() => { 1140 console.info('flush success'); 1141}).catch((err: BusinessError) => { 1142 console.info('flush fail'); 1143}); 1144``` 1145 1146### delete<sup>9+</sup> 1147 1148delete(callback: AsyncCallback\<void\>): void 1149 1150Disables the cache and deletes the data in it. This API uses an asynchronous callback to return the result. 1151 1152**System capability**: SystemCapability.Communication.NetStack 1153 1154**Parameters** 1155 1156| Name | Type | Mandatory| Description | 1157| -------- | --------------------------------------- | ---- | ---------- | 1158| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 1159 1160**Example** 1161 1162```ts 1163import http from '@ohos.net.http'; 1164import { BusinessError } from '@ohos.base'; 1165 1166let httpResponseCache = http.createHttpResponseCache(); 1167httpResponseCache.delete((err: BusinessError) => { 1168 if (err) { 1169 console.info('delete fail'); 1170 return; 1171 } 1172 console.info('delete success'); 1173}); 1174``` 1175 1176### delete<sup>9+</sup> 1177 1178delete(): Promise\<void\> 1179 1180Disables the cache and deletes the data in it. This API uses a promise to return the result. 1181 1182**System capability**: SystemCapability.Communication.NetStack 1183 1184**Return value** 1185 1186| Type | Description | 1187| --------------------------------- | ------------------------------------- | 1188| Promise\<void\> | Promise used to return the result.| 1189 1190**Example** 1191 1192```ts 1193import http from '@ohos.net.http'; 1194import { BusinessError } from '@ohos.base'; 1195 1196let httpResponseCache = http.createHttpResponseCache(); 1197httpResponseCache.delete().then(() => { 1198 console.info('delete success'); 1199}).catch((err: Error) => { 1200 console.info('delete fail'); 1201}); 1202``` 1203 1204## HttpDataType<sup>9+</sup> 1205 1206Enumerates HTTP data types. 1207 1208**System capability**: SystemCapability.Communication.NetStack 1209 1210| Name| Value| Description | 1211| ------------------ | -- | ----------- | 1212| STRING | 0 | String type.| 1213| OBJECT | 1 | Object type. | 1214| ARRAY_BUFFER | 2 | Binary array type.| 1215 1216## HttpProtocol<sup>9+</sup> 1217 1218Enumerates HTTP protocol versions. 1219 1220**System capability**: SystemCapability.Communication.NetStack 1221 1222| Name | Description | 1223| :-------- | :----------- | 1224| HTTP1_1 | HTTP1.1 | 1225| HTTP2 | HTTP2 | 1226