1/* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {AsyncCallback, Callback} from "./basic"; 17import connection from "./@ohos.net.connection"; 18 19/** 20 * Provides http related APIs. 21 * @namespace http 22 * @syscap SystemCapability.Communication.NetStack 23 * @since 6 24 */ 25declare namespace http { 26 /** 27 * @since 10 28 */ 29 type HttpProxy = connection.HttpProxy; 30 31 /** 32 * Creates an HTTP request task. 33 * @returns { HttpRequest } the HttpRequest of the createHttp. 34 * @crossplatform 35 * @syscap SystemCapability.Communication.NetStack 36 * @since 6 37 */ 38 function createHttp(): HttpRequest; 39 40 /** 41 * Specifies the type and value range of the optional parameters in the HTTP request. 42 * @interface HttpRequestOptions 43 * @syscap SystemCapability.Communication.NetStack 44 * @since 6 45 */ 46 export interface HttpRequestOptions { 47 /** 48 * Request method,default is GET. 49 * @type {RequestMethod} 50 * @crossplatform 51 * @since 6 52 */ 53 method?: RequestMethod; 54 55 /** 56 * Additional data of the request. 57 * extraData can be a string or an Object (API 6) or an ArrayBuffer(API 8). 58 * @type {string | Object | ArrayBuffer} 59 * @crossplatform 60 * @since 6 61 */ 62 extraData?: string | Object | ArrayBuffer; 63 64 /** 65 * Data type to be returned. If this parameter is set, the system preferentially returns the specified type. 66 * @type {HttpDataType} 67 * @crossplatform 68 * @since 9 69 */ 70 expectDataType?: HttpDataType; 71 72 /** 73 * default is true 74 * @type {boolean} 75 * @crossplatform 76 * @since 9 77 */ 78 usingCache?: boolean; 79 80 /** 81 * [1, 1000], default is 1. 82 * @type {number} 83 * @crossplatform 84 * @since 9 85 */ 86 priority?: number; 87 88 /** 89 * HTTP request header. default is 'content-type': 'application/json' 90 * @type {Object} 91 * @crossplatform 92 * @since 6 93 */ 94 header?: Object; 95 96 /** 97 * Read timeout period. The default value is 60,000, in ms. 98 * @type {number} 99 * @crossplatform 100 * @since 6 101 */ 102 readTimeout?: number; 103 104 /** 105 * Connection timeout interval. The default value is 60,000, in ms. 106 * @type {number} 107 * @crossplatform 108 * @since 6 109 */ 110 connectTimeout?: number; 111 112 /** 113 * default is automatically specified by the system. 114 * @type {HttpProtocol} 115 * @crossplatform 116 * @since 9 117 */ 118 usingProtocol?: HttpProtocol; 119 /** 120 * If this parameter is set as type of boolean, the system will use default proxy or not use proxy. 121 * If this parameter is set as type of HttpProxy, the system will use the specified HttpProxy. 122 * @type {boolean | HttpProxy} 123 * @since 10 124 */ 125 usingProxy?: boolean | HttpProxy; 126 127 /** 128 * If this parameter is set, the system will use ca path specified by user, or else use preset ca by the system. 129 * @type {string} 130 * @since 10 131 */ 132 caPath?: string; 133 } 134 135 /** 136 * <p>Defines an HTTP request task. Before invoking APIs provided by HttpRequest, 137 * you must call createHttp() to create an HttpRequestTask object.</p> 138 * @interface HttpRequest 139 * @syscap SystemCapability.Communication.NetStack 140 * @since 6 141 */ 142 export interface HttpRequest { 143 /** 144 * Initiates an HTTP request to a given URL. 145 * @param { string } url URL for initiating an HTTP request. 146 * @param { AsyncCallback<HttpResponse> } callback - the callback of request. 147 * @permission ohos.permission.INTERNET 148 * @throws { BusinessError } 401 - Parameter error. 149 * @throws { BusinessError } 201 - Permission denied. 150 * @throws { BusinessError } 2300001 - Unsupported protocol. 151 * @throws { BusinessError } 2300003 - URL using bad/illegal format or missing URL. 152 * @throws { BusinessError } 2300005 - Couldn't resolve proxy name. 153 * @throws { BusinessError } 2300006 - Couldn't resolve host name. 154 * @throws { BusinessError } 2300007 - Couldn't connect to server. 155 * @throws { BusinessError } 2300008 - Weird server reply. 156 * @throws { BusinessError } 2300009 - Access denied to remote resource. 157 * @throws { BusinessError } 2300016 - Error in the HTTP2 framing layer. 158 * @throws { BusinessError } 2300018 - Transferred a partial file. 159 * @throws { BusinessError } 2300023 - Failed writing received data to disk/application. 160 * @throws { BusinessError } 2300025 - Upload failed. 161 * @throws { BusinessError } 2300026 - Failed to open/read local data from file/application. 162 * @throws { BusinessError } 2300027 - Out of memory. 163 * @throws { BusinessError } 2300028 - Timeout was reached. 164 * @throws { BusinessError } 2300047 - Number of redirects hit maximum amount. 165 * @throws { BusinessError } 2300052 - Server returned nothing (no headers, no data). 166 * @throws { BusinessError } 2300055 - Failed sending data to the peer. 167 * @throws { BusinessError } 2300056 - Failure when receiving data from the peer. 168 * @throws { BusinessError } 2300058 - Problem with the local SSL certificate. 169 * @throws { BusinessError } 2300059 - Couldn't use specified SSL cipher. 170 * @throws { BusinessError } 2300060 - SSL peer certificate or SSH remote key was not OK. 171 * @throws { BusinessError } 2300061 - Unrecognized or bad HTTP Content or Transfer-Encoding. 172 * @throws { BusinessError } 2300063 - Maximum file size exceeded. 173 * @throws { BusinessError } 2300070 - Disk full or allocation exceeded. 174 * @throws { BusinessError } 2300073 - Remote file already exists. 175 * @throws { BusinessError } 2300077 - Problem with the SSL CA cert (path? access rights?). 176 * @throws { BusinessError } 2300078 - Remote file not found. 177 * @throws { BusinessError } 2300094 - An authentication function returned an error. 178 * @throws { BusinessError } 2300999 - Unknown Other Error. 179 * @syscap SystemCapability.Communication.NetStack 180 * @crossplatform 181 * @since 6 182 */ 183 request(url: string, callback: AsyncCallback<HttpResponse>): void; 184 185 /** 186 * Initiates an HTTP request to a given URL. 187 * @param { string } url URL for initiating an HTTP request. 188 * @param { HttpRequestOptions } options Optional parameters {@link HttpRequestOptions}. 189 * @param { AsyncCallback<HttpResponse> } callback callback - the callback of request.. 190 * @permission ohos.permission.INTERNET 191 * @throws { BusinessError } 401 - Parameter error. 192 * @throws { BusinessError } 201 - Permission denied. 193 * @throws { BusinessError } 2300001 - Unsupported protocol. 194 * @throws { BusinessError } 2300003 - URL using bad/illegal format or missing URL. 195 * @throws { BusinessError } 2300005 - Couldn't resolve proxy name. 196 * @throws { BusinessError } 2300006 - Couldn't resolve host name. 197 * @throws { BusinessError } 2300007 - Couldn't connect to server. 198 * @throws { BusinessError } 2300008 - Weird server reply. 199 * @throws { BusinessError } 2300009 - Access denied to remote resource. 200 * @throws { BusinessError } 2300016 - Error in the HTTP2 framing layer. 201 * @throws { BusinessError } 2300018 - Transferred a partial file. 202 * @throws { BusinessError } 2300023 - Failed writing received data to disk/application. 203 * @throws { BusinessError } 2300025 - Upload failed. 204 * @throws { BusinessError } 2300026 - Failed to open/read local data from file/application. 205 * @throws { BusinessError } 2300027 - Out of memory. 206 * @throws { BusinessError } 2300028 - Timeout was reached. 207 * @throws { BusinessError } 2300047 - Number of redirects hit maximum amount. 208 * @throws { BusinessError } 2300052 - Server returned nothing (no headers, no data). 209 * @throws { BusinessError } 2300055 - Failed sending data to the peer. 210 * @throws { BusinessError } 2300056 - Failure when receiving data from the peer. 211 * @throws { BusinessError } 2300058 - Problem with the local SSL certificate. 212 * @throws { BusinessError } 2300059 - Couldn't use specified SSL cipher. 213 * @throws { BusinessError } 2300060 - SSL peer certificate or SSH remote key was not OK. 214 * @throws { BusinessError } 2300061 - Unrecognized or bad HTTP Content or Transfer-Encoding. 215 * @throws { BusinessError } 2300063 - Maximum file size exceeded. 216 * @throws { BusinessError } 2300070 - Disk full or allocation exceeded. 217 * @throws { BusinessError } 2300073 - Remote file already exists. 218 * @throws { BusinessError } 2300077 - Problem with the SSL CA cert (path? access rights?). 219 * @throws { BusinessError } 2300078 - Remote file not found. 220 * @throws { BusinessError } 2300094 - An authentication function returned an error. 221 * @throws { BusinessError } 2300999 - Unknown Other Error. 222 * @syscap SystemCapability.Communication.NetStack 223 * @crossplatform 224 * @since 6 225 */ 226 request(url: string, options: HttpRequestOptions, callback: AsyncCallback<HttpResponse>): void; 227 228 /** 229 * Initiates an HTTP request to a given URL. 230 * @param { string } url URL for initiating an HTTP request. 231 * @param { HttpRequestOptions } options Optional parameters {@link HttpRequestOptions}. 232 * @returns { Promise<HttpResponse> } The promise returned by the function. 233 * @permission ohos.permission.INTERNET 234 * @throws { BusinessError } 401 - Parameter error. 235 * @throws { BusinessError } 201 - Permission denied. 236 * @throws { BusinessError } 2300001 - Unsupported protocol. 237 * @throws { BusinessError } 2300003 - URL using bad/illegal format or missing URL. 238 * @throws { BusinessError } 2300005 - Couldn't resolve proxy name. 239 * @throws { BusinessError } 2300006 - Couldn't resolve host name. 240 * @throws { BusinessError } 2300007 - Couldn't connect to server. 241 * @throws { BusinessError } 2300008 - Weird server reply. 242 * @throws { BusinessError } 2300009 - Access denied to remote resource. 243 * @throws { BusinessError } 2300016 - Error in the HTTP2 framing layer. 244 * @throws { BusinessError } 2300018 - Transferred a partial file. 245 * @throws { BusinessError } 2300023 - Failed writing received data to disk/application. 246 * @throws { BusinessError } 2300025 - Upload failed. 247 * @throws { BusinessError } 2300026 - Failed to open/read local data from file/application. 248 * @throws { BusinessError } 2300027 - Out of memory. 249 * @throws { BusinessError } 2300028 - Timeout was reached. 250 * @throws { BusinessError } 2300047 - Number of redirects hit maximum amount. 251 * @throws { BusinessError } 2300052 - Server returned nothing (no headers, no data). 252 * @throws { BusinessError } 2300055 - Failed sending data to the peer. 253 * @throws { BusinessError } 2300056 - Failure when receiving data from the peer. 254 * @throws { BusinessError } 2300058 - Problem with the local SSL certificate. 255 * @throws { BusinessError } 2300059 - Couldn't use specified SSL cipher. 256 * @throws { BusinessError } 2300060 - SSL peer certificate or SSH remote key was not OK. 257 * @throws { BusinessError } 2300061 - Unrecognized or bad HTTP Content or Transfer-Encoding. 258 * @throws { BusinessError } 2300063 - Maximum file size exceeded. 259 * @throws { BusinessError } 2300070 - Disk full or allocation exceeded. 260 * @throws { BusinessError } 2300073 - Remote file already exists. 261 * @throws { BusinessError } 2300077 - Problem with the SSL CA cert (path? access rights?). 262 * @throws { BusinessError } 2300078 - Remote file not found. 263 * @throws { BusinessError } 2300094 - An authentication function returned an error. 264 * @throws { BusinessError } 2300999 - Unknown Other Error. 265 * @syscap SystemCapability.Communication.NetStack 266 * @crossplatform 267 * @since 6 268 */ 269 request(url: string, options?: HttpRequestOptions): Promise<HttpResponse>; 270 271 /** 272 * Initiates an HTTP request to a given URL, applicable to scenarios where http response supports streaming. 273 * @param { string } url URL for initiating an HTTP request. 274 * <p>@param { AsyncCallback<number> } callback Returns the callback of requestInStream {@link ResponseCode}, 275 * should use on_headersReceive and on_dataReceive to get http response.</p> 276 * @permission ohos.permission.INTERNET 277 * @throws { BusinessError } 401 - Parameter error. 278 * @throws { BusinessError } 201 - Permission denied. 279 * @throws { BusinessError } 2300001 - Unsupported protocol. 280 * @throws { BusinessError } 2300003 - URL using bad/illegal format or missing URL. 281 * @throws { BusinessError } 2300005 - Couldn't resolve proxy name. 282 * @throws { BusinessError } 2300006 - Couldn't resolve host name. 283 * @throws { BusinessError } 2300007 - Couldn't connect to server. 284 * @throws { BusinessError } 2300008 - Weird server reply. 285 * @throws { BusinessError } 2300009 - Access denied to remote resource. 286 * @throws { BusinessError } 2300016 - Error in the HTTP2 framing layer. 287 * @throws { BusinessError } 2300018 - Transferred a partial file. 288 * @throws { BusinessError } 2300023 - Failed writing received data to disk/application. 289 * @throws { BusinessError } 2300025 - Upload failed. 290 * @throws { BusinessError } 2300026 - Failed to open/read local data from file/application. 291 * @throws { BusinessError } 2300027 - Out of memory. 292 * @throws { BusinessError } 2300028 - Timeout was reached. 293 * @throws { BusinessError } 2300047 - Number of redirects hit maximum amount. 294 * @throws { BusinessError } 2300052 - Server returned nothing (no headers, no data). 295 * @throws { BusinessError } 2300055 - Failed sending data to the peer. 296 * @throws { BusinessError } 2300056 - Failure when receiving data from the peer. 297 * @throws { BusinessError } 2300058 - Problem with the local SSL certificate. 298 * @throws { BusinessError } 2300059 - Couldn't use specified SSL cipher. 299 * @throws { BusinessError } 2300060 - SSL peer certificate or SSH remote key was not OK. 300 * @throws { BusinessError } 2300061 - Unrecognized or bad HTTP Content or Transfer-Encoding. 301 * @throws { BusinessError } 2300063 - Maximum file size exceeded. 302 * @throws { BusinessError } 2300070 - Disk full or allocation exceeded. 303 * @throws { BusinessError } 2300073 - Remote file already exists. 304 * @throws { BusinessError } 2300077 - Problem with the SSL CA cert (path? access rights?). 305 * @throws { BusinessError } 2300078 - Remote file not found. 306 * @throws { BusinessError } 2300094 - An authentication function returned an error. 307 * @throws { BusinessError } 2300999 - Unknown Other Error. 308 * @syscap SystemCapability.Communication.NetStack 309 * @since 10 310 */ 311 requestInStream(url: string, callback: AsyncCallback<number>): void; 312 313 /** 314 * Initiates an HTTP request to a given URL, applicable to scenarios where http response supports streaming. 315 * @param { string } url URL for initiating an HTTP request. 316 * @param { HttpRequestOptions } options Optional parameters {@link HttpRequestOptions}. 317 * @param { AsyncCallback<number> } callback - the callback of requestInStream. 318 * @permission ohos.permission.INTERNET 319 * @throws { BusinessError } 401 - Parameter error. 320 * @throws { BusinessError } 201 - Permission denied. 321 * @throws { BusinessError } 2300001 - Unsupported protocol. 322 * @throws { BusinessError } 2300003 - URL using bad/illegal format or missing URL. 323 * @throws { BusinessError } 2300005 - Couldn't resolve proxy name. 324 * @throws { BusinessError } 2300006 - Couldn't resolve host name. 325 * @throws { BusinessError } 2300007 - Couldn't connect to server. 326 * @throws { BusinessError } 2300008 - Weird server reply. 327 * @throws { BusinessError } 2300009 - Access denied to remote resource. 328 * @throws { BusinessError } 2300016 - Error in the HTTP2 framing layer. 329 * @throws { BusinessError } 2300018 - Transferred a partial file. 330 * @throws { BusinessError } 2300023 - Failed writing received data to disk/application. 331 * @throws { BusinessError } 2300025 - Upload failed. 332 * @throws { BusinessError } 2300026 - Failed to open/read local data from file/application. 333 * @throws { BusinessError } 2300027 - Out of memory. 334 * @throws { BusinessError } 2300028 - Timeout was reached. 335 * @throws { BusinessError } 2300047 - Number of redirects hit maximum amount. 336 * @throws { BusinessError } 2300052 - Server returned nothing (no headers, no data). 337 * @throws { BusinessError } 2300055 - Failed sending data to the peer. 338 * @throws { BusinessError } 2300056 - Failure when receiving data from the peer. 339 * @throws { BusinessError } 2300058 - Problem with the local SSL certificate. 340 * @throws { BusinessError } 2300059 - Couldn't use specified SSL cipher. 341 * @throws { BusinessError } 2300060 - SSL peer certificate or SSH remote key was not OK. 342 * @throws { BusinessError } 2300061 - Unrecognized or bad HTTP Content or Transfer-Encoding. 343 * @throws { BusinessError } 2300063 - Maximum file size exceeded. 344 * @throws { BusinessError } 2300070 - Disk full or allocation exceeded. 345 * @throws { BusinessError } 2300073 - Remote file already exists. 346 * @throws { BusinessError } 2300077 - Problem with the SSL CA cert (path? access rights?). 347 * @throws { BusinessError } 2300078 - Remote file not found. 348 * @throws { BusinessError } 2300094 - An authentication function returned an error. 349 * @throws { BusinessError } 2300999 - Unknown Other Error. 350 * @syscap SystemCapability.Communication.NetStack 351 * @since 10 352 */ 353 requestInStream(url: string, options: HttpRequestOptions, callback: AsyncCallback<number>): void; 354 355 /** 356 * Initiates an HTTP request to a given URL, applicable to scenarios where http response supports streaming. 357 * @param { string } url URL for initiating an HTTP request. 358 * @param { HttpRequestOptions } options Optional parameters {@link HttpRequestOptions}. 359 * @returns { Promise<number> } the promise returned by the function. 360 * @permission ohos.permission.INTERNET 361 * @throws { BusinessError } 401 - Parameter error. 362 * @throws { BusinessError } 201 - Permission denied. 363 * @throws { BusinessError } 2300001 - Unsupported protocol. 364 * @throws { BusinessError } 2300003 - URL using bad/illegal format or missing URL. 365 * @throws { BusinessError } 2300005 - Couldn't resolve proxy name. 366 * @throws { BusinessError } 2300006 - Couldn't resolve host name. 367 * @throws { BusinessError } 2300007 - Couldn't connect to server. 368 * @throws { BusinessError } 2300008 - Weird server reply. 369 * @throws { BusinessError } 2300009 - Access denied to remote resource. 370 * @throws { BusinessError } 2300016 - Error in the HTTP2 framing layer. 371 * @throws { BusinessError } 2300018 - Transferred a partial file. 372 * @throws { BusinessError } 2300023 - Failed writing received data to disk/application. 373 * @throws { BusinessError } 2300025 - Upload failed. 374 * @throws { BusinessError } 2300026 - Failed to open/read local data from file/application. 375 * @throws { BusinessError } 2300027 - Out of memory. 376 * @throws { BusinessError } 2300028 - Timeout was reached. 377 * @throws { BusinessError } 2300047 - Number of redirects hit maximum amount. 378 * @throws { BusinessError } 2300052 - Server returned nothing (no headers, no data). 379 * @throws { BusinessError } 2300055 - Failed sending data to the peer. 380 * @throws { BusinessError } 2300056 - Failure when receiving data from the peer. 381 * @throws { BusinessError } 2300058 - Problem with the local SSL certificate. 382 * @throws { BusinessError } 2300059 - Couldn't use specified SSL cipher. 383 * @throws { BusinessError } 2300060 - SSL peer certificate or SSH remote key was not OK. 384 * @throws { BusinessError } 2300061 - Unrecognized or bad HTTP Content or Transfer-Encoding. 385 * @throws { BusinessError } 2300063 - Maximum file size exceeded. 386 * @throws { BusinessError } 2300070 - Disk full or allocation exceeded. 387 * @throws { BusinessError } 2300073 - Remote file already exists. 388 * @throws { BusinessError } 2300077 - Problem with the SSL CA cert (path? access rights?). 389 * @throws { BusinessError } 2300078 - Remote file not found. 390 * @throws { BusinessError } 2300094 - An authentication function returned an error. 391 * @throws { BusinessError } 2300999 - Unknown Other Error. 392 * @syscap SystemCapability.Communication.NetStack 393 * @since 10 394 */ 395 requestInStream(url: string, options?: HttpRequestOptions): Promise<number>; 396 397 /** 398 * Destroys an HTTP request. 399 * @syscap SystemCapability.Communication.NetStack 400 * @crossplatform 401 */ 402 destroy(): void; 403 404 /** 405 * Registers an observer for HTTP Response Header events. 406 * @param { string } type Indicates Event name. 407 * @param { AsyncCallback<Object> } callback - the callback of on. 408 * @syscap SystemCapability.Communication.NetStack 409 * @deprecated since 8 410 * @useinstead on_headersReceive 411 */ 412 on(type: 'headerReceive', callback: AsyncCallback<Object>): void; 413 414 /** 415 * Unregisters the observer for HTTP Response Header events. 416 * @param { string } type Indicates Event name. 417 * @param { AsyncCallback<Object> } callback - the callback of off. 418 * @syscap SystemCapability.Communication.NetStack 419 * @deprecated since 8 420 * @useinstead off_headersReceive 421 */ 422 off(type: 'headerReceive', callback?: AsyncCallback<Object>): void; 423 424 /** 425 * Registers an observer for HTTP Response Header events. 426 * @param { string } type Indicates Event name. 427 * @param { Callback<Object> } callback - the callback of on. 428 * @syscap SystemCapability.Communication.NetStack 429 * @crossplatform 430 * @since 8 431 */ 432 on(type: 'headersReceive', callback: Callback<Object>): void; 433 434 /** 435 * Unregisters the observer for HTTP Response Header events. 436 * @param { string } type Indicates Event name. 437 * @param { Callback<Object> } callback - the callback of off. 438 * @syscap SystemCapability.Communication.NetStack 439 * @crossplatform 440 * @since 8 441 */ 442 off(type: 'headersReceive', callback?: Callback<Object>): void; 443 444 /** 445 * Registers a one-time observer for HTTP Response Header events. 446 * @param { string } type Indicates Event name. 447 * @param { Callback<Object> } callback - the callback of once. 448 * @syscap SystemCapability.Communication.NetStack 449 * @crossplatform 450 * @since 8 451 */ 452 once(type: 'headersReceive', callback: Callback<Object>): void; 453 454 /** 455 * Registers an observer for receiving HTTP Response data events continuously. 456 * @param { string } type Indicates Event name. 457 * @param { Callback<ArrayBuffer> } callback - the callback of on. 458 * @syscap SystemCapability.Communication.NetStack 459 * @since 10 460 */ 461 on(type: 'dataReceive', callback: Callback<ArrayBuffer>): void; 462 463 /** 464 * Unregisters an observer for receiving HTTP Response data events continuously. 465 * @param { string } type Indicates Event name. 466 * @param { Callback<ArrayBuffer> } callback - the callback of off. 467 * @syscap SystemCapability.Communication.NetStack 468 * @since 10 469 */ 470 off(type: 'dataReceive', callback?: Callback<ArrayBuffer>): void; 471 472 /** 473 * Registers an observer for receiving HTTP Response data ends events. 474 * @param { string } type Indicates Event name. 475 * @param { Callback<void> } callback - the callback of on. 476 * @syscap SystemCapability.Communication.NetStack 477 * @since 10 478 */ 479 on(type: 'dataEnd', callback: Callback<void>): void; 480 481 /** 482 * Unregisters an observer for receiving HTTP Response data ends events. 483 * @param { string } type Indicates Event name. 484 * @param { Callback<void> } callback - the callback of off. 485 * @syscap SystemCapability.Communication.NetStack 486 * @since 10 487 */ 488 off(type: 'dataEnd', callback?: Callback<void>): void; 489 490 /** 491 * Registers an observer for progress of receiving HTTP Response data events. 492 * @param { string } type Indicates Event name. 493 * @param { Callback<{ receiveSize: number, totalSize: number }> } callback - the callback of on. 494 * @syscap SystemCapability.Communication.NetStack 495 * @since 10 496 */ 497 on(type: 'dataReceiveProgress', callback: Callback<{ receiveSize: number, totalSize: number }>): void; 498 499 /** 500 * Unregisters an observer for progress of receiving HTTP Response data events. 501 * @param { string } type Indicates Event name. 502 * @param { Callback<{ receiveSize: number, totalSize: number }> } callback - the callback of off. 503 * @syscap SystemCapability.Communication.NetStack 504 * @since 10 505 */ 506 off(type: 'dataReceiveProgress', callback?: Callback<{ receiveSize: number, totalSize: number }>): void; 507 } 508 509 /** 510 * Defines an HTTP request method. 511 * @enum {string} 512 * @syscap SystemCapability.Communication.NetStack 513 * @crossplatform 514 * @since 6 515 */ 516 export enum RequestMethod { 517 /** 518 * OPTIONS method. 519 * @syscap SystemCapability.Communication.NetStack 520 * @since 6 521 */ 522 OPTIONS = "OPTIONS", 523 524 /** 525 * GET method. 526 * @syscap SystemCapability.Communication.NetStack 527 * @since 6 528 */ 529 GET = "GET", 530 531 /** 532 * HEAD method. 533 * @syscap SystemCapability.Communication.NetStack 534 * @since 6 535 */ 536 HEAD = "HEAD", 537 538 /** 539 * POST method. 540 * @syscap SystemCapability.Communication.NetStack 541 * @since 6 542 */ 543 POST = "POST", 544 545 /** 546 * PUT method. 547 * @syscap SystemCapability.Communication.NetStack 548 * @since 6 549 */ 550 PUT = "PUT", 551 552 /** 553 * DELETE method. 554 * @syscap SystemCapability.Communication.NetStack 555 * @since 6 556 */ 557 DELETE = "DELETE", 558 559 /** 560 * TRACE method. 561 * @syscap SystemCapability.Communication.NetStack 562 * @since 6 563 */ 564 TRACE = "TRACE", 565 566 /** 567 * CONNECT method. 568 * @syscap SystemCapability.Communication.NetStack 569 * @since 6 570 */ 571 CONNECT = "CONNECT" 572 } 573 574 /** 575 * Enumerates the response codes for an HTTP request. 576 * @syscap SystemCapability.Communication.NetStack 577 * @crossplatform 578 * @since 6 579 */ 580 export enum ResponseCode { 581 /** 582 * The request was successful. Typically used for GET and POST requests. 583 * @syscap SystemCapability.Communication.NetStack 584 * @since 6 585 */ 586 OK = 200, 587 588 /** 589 * Successfully requested and created a new resource. 590 * @syscap SystemCapability.Communication.NetStack 591 * @since 6 592 */ 593 594 CREATED, 595 596 /** 597 * The request has been accepted but has not been processed completely. 598 * @syscap SystemCapability.Communication.NetStack 599 * @since 6 600 */ 601 ACCEPTED, 602 603 /** 604 * Unauthorized information. The request was successful. 605 * @syscap SystemCapability.Communication.NetStack 606 * @since 6 607 */ 608 NOT_AUTHORITATIVE, 609 610 /** 611 * No content. The server successfully processed, but did not return content. 612 * @syscap SystemCapability.Communication.NetStack 613 * @since 6 614 */ 615 NO_CONTENT, 616 617 /** 618 * Reset the content. 619 * @syscap SystemCapability.Communication.NetStack 620 * @since 6 621 */ 622 RESET, 623 624 /** 625 * Partial content. The server successfully processed some GET requests. 626 * @syscap SystemCapability.Communication.NetStack 627 * @since 6 628 */ 629 PARTIAL, 630 631 /** 632 * Multiple options. 633 * @syscap SystemCapability.Communication.NetStack 634 * @since 6 635 */ 636 MULT_CHOICE = 300, 637 638 /** 639 * <p>Permanently move. The requested resource has been permanently moved to a new URI, 640 * and the returned information will include the new URI. The browser will automatically redirect to the new URI.</p> 641 * @syscap SystemCapability.Communication.NetStack 642 * @since 6 643 */ 644 MOVED_PERM, 645 646 /** 647 * Temporary movement. 648 * @syscap SystemCapability.Communication.NetStack 649 * @since 6 650 */ 651 MOVED_TEMP, 652 653 /** 654 * View other addresses. 655 * @syscap SystemCapability.Communication.NetStack 656 * @since 6 657 */ 658 SEE_OTHER, 659 660 /** 661 * Not modified. 662 * @syscap SystemCapability.Communication.NetStack 663 * @since 6 664 */ 665 NOT_MODIFIED, 666 667 /** 668 * Using proxies. 669 * @syscap SystemCapability.Communication.NetStack 670 * @since 6 671 */ 672 USE_PROXY, 673 674 /** 675 * The server cannot understand the syntax error error requested by the client. 676 * @syscap SystemCapability.Communication.NetStack 677 * @since 6 678 */ 679 BAD_REQUEST = 400, 680 681 /** 682 * Request for user authentication. 683 * @syscap SystemCapability.Communication.NetStack 684 * @since 6 685 */ 686 UNAUTHORIZED, 687 688 /** 689 * Reserved for future use. 690 * @syscap SystemCapability.Communication.NetStack 691 * @since 6 692 */ 693 PAYMENT_REQUIRED, 694 695 /** 696 * The server understands the request from the requesting client, but refuses to execute it. 697 * @syscap SystemCapability.Communication.NetStack 698 * @since 6 699 */ 700 FORBIDDEN, 701 702 /** 703 * The server was unable to find resources (web pages) based on the client's request. 704 * @syscap SystemCapability.Communication.NetStack 705 * @since 6 706 */ 707 NOT_FOUND, 708 709 /** 710 * The method in the client request is prohibited. 711 * @syscap SystemCapability.Communication.NetStack 712 * @since 6 713 */ 714 BAD_METHOD, 715 716 /** 717 * The server is unable to complete the request based on the content characteristics requested by the client. 718 * @syscap SystemCapability.Communication.NetStack 719 * @since 6 720 */ 721 NOT_ACCEPTABLE, 722 723 /** 724 * Request authentication of the proxy's identity. 725 * @syscap SystemCapability.Communication.NetStack 726 * @since 6 727 */ 728 PROXY_AUTH, 729 730 /** 731 * The request took too long and timed out. 732 * @syscap SystemCapability.Communication.NetStack 733 * @since 6 734 */ 735 CLIENT_TIMEOUT, 736 /** 737 * <p>The server may have returned this code when completing the client's PUT request, 738 * as there was a conflict when the server was processing the request.</p> 739 * @syscap SystemCapability.Communication.NetStack 740 * @since 6 741 */ 742 CONFLICT, 743 744 /** 745 * The resource requested by the client no longer exists. 746 * @syscap SystemCapability.Communication.NetStack 747 * @since 6 748 */ 749 GONE, 750 751 /** 752 * The server is unable to process request information sent by the client without Content Length. 753 * @syscap SystemCapability.Communication.NetStack 754 * @since 6 755 */ 756 LENGTH_REQUIRED, 757 758 /** 759 * The prerequisite for requesting information from the client is incorrect. 760 * @syscap SystemCapability.Communication.NetStack 761 * @since 6 762 */ 763 PRECON_FAILED, 764 765 /** 766 * The request was rejected because the requested entity was too large for the server to process. 767 * @syscap SystemCapability.Communication.NetStack 768 * @since 6 769 */ 770 ENTITY_TOO_LARGE, 771 772 /** 773 * The requested URI is too long (usually a URL) and the server cannot process it. 774 * @syscap SystemCapability.Communication.NetStack 775 * @since 6 776 */ 777 REQ_TOO_LONG, 778 779 /** 780 * The server is unable to process the requested format. 781 * @syscap SystemCapability.Communication.NetStack 782 * @since 6 783 */ 784 UNSUPPORTED_TYPE, 785 786 /** 787 * Internal server error, unable to complete the request. 788 * @syscap SystemCapability.Communication.NetStack 789 * @since 6 790 */ 791 INTERNAL_ERROR = 500, 792 793 /** 794 * The server does not support the requested functionality and cannot complete the request. 795 * @syscap SystemCapability.Communication.NetStack 796 * @since 6 797 */ 798 NOT_IMPLEMENTED, 799 800 /** 801 * The server acting as a gateway or proxy received an invalid request from the remote server. 802 * @syscap SystemCapability.Communication.NetStack 803 * @since 6 804 */ 805 BAD_GATEWAY, 806 807 /** 808 * Due to overload or system maintenance, the server is temporarily unable to process client requests. 809 * @syscap SystemCapability.Communication.NetStack 810 * @since 6 811 */ 812 UNAVAILABLE, 813 814 /** 815 * The server acting as a gateway or proxy did not obtain requests from the remote server in a timely manner. 816 * @syscap SystemCapability.Communication.NetStack 817 * @since 6 818 */ 819 GATEWAY_TIMEOUT, 820 821 /** 822 * The version of the HTTP protocol requested by the server. 823 * @syscap SystemCapability.Communication.NetStack 824 * @since 6 825 */ 826 VERSION 827 } 828 829 /** 830 * Supported protocols. 831 * @syscap SystemCapability.Communication.NetStack 832 * @crossplatform 833 * @since 9 834 */ 835 export enum HttpProtocol { 836 /** 837 * Protocol http1.1 838 * @syscap SystemCapability.Communication.NetStack 839 * @since 9 840 */ 841 HTTP1_1, 842 843 /** 844 * Protocol http2 845 * @syscap SystemCapability.Communication.NetStack 846 * @since 9 847 */ 848 HTTP2, 849 } 850 851 /** 852 * Indicates the type of the returned data. 853 * @syscap SystemCapability.Communication.NetStack 854 * @crossplatform 855 * @since 9 856 */ 857 export enum HttpDataType { 858 /** 859 * The returned type is string. 860 * @syscap SystemCapability.Communication.NetStack 861 * @crossplatform 862 * @since 6 863 */ 864 STRING, 865 /** 866 * The returned type is Object. 867 * @syscap SystemCapability.Communication.NetStack 868 * @crossplatform 869 * @since 6 870 */ 871 OBJECT = 1, 872 /** 873 * The returned type is ArrayBuffer. 874 * @syscap SystemCapability.Communication.NetStack 875 * @crossplatform 876 * @since 6 877 */ 878 ARRAY_BUFFER = 2, 879 } 880 881 /** 882 * Defines the response to an HTTP request. 883 * @interface HttpResponse 884 * @syscap SystemCapability.Communication.NetStack 885 * @crossplatform 886 * @since 6 887 */ 888 export interface HttpResponse { 889 /** 890 * result can be a string (API 6) or an ArrayBuffer(API 8). Object is deprecated from API 8. 891 * If {@link HttpRequestOptions#expectDataType} is set, the system preferentially returns this parameter. 892 * @type {string | Object | ArrayBuffer} 893 * @crossplatform 894 * @since 6 895 */ 896 result: string | Object | ArrayBuffer; 897 898 /** 899 * If the resultType is string, you can get result directly. 900 * If the resultType is Object, you can get result such as this: result['key']. 901 * If the resultType is ArrayBuffer, you can use ArrayBuffer to create the binary objects. 902 * @type {HttpDataType} 903 * @crossplatform 904 * @since 9 905 */ 906 resultType: HttpDataType; 907 908 /** 909 * Server status code. 910 * @type {ResponseCode | number} 911 * @crossplatform 912 * @since 6 913 */ 914 responseCode: ResponseCode | number; 915 916 /** 917 * All headers in the response from the server. 918 * @type {Object} 919 * @crossplatform 920 * @since 6 921 */ 922 header: Object; 923 924 /** 925 * Cookies returned by the server. 926 * @type {string} 927 * @crossplatform 928 * @since 8 929 */ 930 cookies: string; 931 } 932 933 /** 934 * Creates a default {@code HttpResponseCache} object to store the responses of HTTP access requests. 935 * @param { number } cacheSize the size of cache(max value is 10MB), default is 10*1024*1024(10MB). 936 * @returns { HttpResponseCache } the HttpResponseCache of the createHttpResponseCache. 937 * @syscap SystemCapability.Communication.NetStack 938 * @crossplatform 939 * @since 9 940 */ 941 function createHttpResponseCache(cacheSize?: number): HttpResponseCache; 942 943 /** 944 * Defines an object that stores the response to an HTTP request. 945 * @interface HttpResponseCache 946 * @syscap SystemCapability.Communication.NetStack 947 * @crossplatform 948 * @since 9 949 */ 950 export interface HttpResponseCache { 951 /** 952 * Writes data in the cache to the file system so that all the cached data can be accessed in the next HTTP request. 953 * @param { AsyncCallback<void> } callback Returns the callback of flush. 954 * @systemapi Hide this for inner system use. 955 * @syscap SystemCapability.Communication.NetStack 956 * @crossplatform 957 * @since 9 958 */ 959 flush(callback: AsyncCallback<void>): void; 960 961 /** 962 * Writes data in the cache to the file system so that all the cached data can be accessed in the next HTTP request. 963 * @returns { Promise<void> } The promise returned by the flush. 964 * @systemapi Hide this for inner system use. 965 * @syscap SystemCapability.Communication.NetStack 966 * @crossplatform 967 * @since 9 968 */ 969 flush(): Promise<void>; 970 971 /** 972 * Disables a cache and deletes the data in it. 973 * @param { AsyncCallback<void> } callback Returns the callback of delete. 974 * @systemapi Hide this for inner system use. 975 * @syscap SystemCapability.Communication.NetStack 976 * @crossplatform 977 * @since 9 978 */ 979 delete(callback: AsyncCallback<void>): void; 980 981 /** 982 * Disables a cache and deletes the data in it. 983 * @returns { Promise<void> } The promise returned by the delete. 984 * @systemapi Hide this for inner system use. 985 * @syscap SystemCapability.Communication.NetStack 986 * @crossplatform 987 * @since 9 988 */ 989 delete(): Promise<void>; 990 } 991} 992 993export default http;