• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2021-2022 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";
17
18/**
19 * Provides http related APIs.
20 *
21 * @since 6
22 * @syscap SystemCapability.Communication.NetStack
23 */
24declare namespace http {
25  /**
26   * Creates an HTTP request task.
27   */
28  function createHttp(): HttpRequest;
29
30  export interface HttpRequestOptions {
31    /**
32     * Request method.
33     */
34    method?: RequestMethod; // default is GET
35    /**
36     * Additional data of the request.
37     * extraData can be a string or an Object (API 6) or an ArrayBuffer(API 8).
38     */
39    extraData?: string | Object | ArrayBuffer;
40    /**
41     * HTTP request header.
42     */
43    header?: Object; // default is 'content-type': 'application/json'
44    /**
45     * Read timeout period. The default value is 60,000, in ms.
46     */
47    readTimeout?: number; // default is 60s
48    /**
49     * Connection timeout interval. The default value is 60,000, in ms.
50     */
51    connectTimeout?: number; // default is 60s.
52  }
53
54  export interface HttpRequest {
55    /**
56     * Initiates an HTTP request to a given URL.
57     *
58     * @param url URL for initiating an HTTP request.
59     * @param options Optional parameters {@link HttpRequestOptions}.
60     * @param callback Returns {@link HttpResponse}.
61     * @permission ohos.permission.INTERNET
62     */
63    request(url: string, callback: AsyncCallback<HttpResponse>): void;
64    request(url: string, options: HttpRequestOptions, callback: AsyncCallback<HttpResponse>): void;
65    request(url: string, options?: HttpRequestOptions): Promise<HttpResponse>;
66
67    /**
68     * Destroys an HTTP request.
69     */
70    destroy(): void;
71
72    /**
73     * Registers an observer for HTTP Response Header events.
74     *
75     * @deprecated use on_headersReceive instead since 8.
76     */
77    on(type: "headerReceive", callback: AsyncCallback<Object>): void;
78
79    /**
80     * Unregisters the observer for HTTP Response Header events.
81     *
82     * @deprecated use off_headersReceive instead since 8.
83     */
84    off(type: "headerReceive", callback?: AsyncCallback<Object>): void;
85
86    /**
87     * Registers an observer for HTTP Response Header events.
88     *
89     * @since 8
90     */
91    on(type: "headersReceive", callback: Callback<Object>): void;
92
93    /**
94     * Unregisters the observer for HTTP Response Header events.
95     *
96     * @since 8
97     */
98    off(type: "headersReceive", callback?: Callback<Object>): void;
99
100    /**
101     * Registers a one-time observer for HTTP Response Header events.
102     *
103     * @since 8
104     */
105    once(type: "headersReceive", callback: Callback<Object>): void;
106  }
107
108  export enum RequestMethod {
109    OPTIONS = "OPTIONS",
110    GET = "GET",
111    HEAD = "HEAD",
112    POST = "POST",
113    PUT = "PUT",
114    DELETE = "DELETE",
115    TRACE = "TRACE",
116    CONNECT = "CONNECT"
117  }
118
119  export enum ResponseCode {
120    OK = 200,
121    CREATED,
122    ACCEPTED,
123    NOT_AUTHORITATIVE,
124    NO_CONTENT,
125    RESET,
126    PARTIAL,
127    MULT_CHOICE = 300,
128    MOVED_PERM,
129    MOVED_TEMP,
130    SEE_OTHER,
131    NOT_MODIFIED,
132    USE_PROXY,
133    BAD_REQUEST = 400,
134    UNAUTHORIZED,
135    PAYMENT_REQUIRED,
136    FORBIDDEN,
137    NOT_FOUND,
138    BAD_METHOD,
139    NOT_ACCEPTABLE,
140    PROXY_AUTH,
141    CLIENT_TIMEOUT,
142    CONFLICT,
143    GONE,
144    LENGTH_REQUIRED,
145    PRECON_FAILED,
146    ENTITY_TOO_LARGE,
147    REQ_TOO_LONG,
148    UNSUPPORTED_TYPE,
149    INTERNAL_ERROR = 500,
150    NOT_IMPLEMENTED,
151    BAD_GATEWAY,
152    UNAVAILABLE,
153    GATEWAY_TIMEOUT,
154    VERSION
155  }
156
157  export interface HttpResponse {
158    /**
159     * result can be a string (API 6) or an ArrayBuffer(API 8). Object is deprecated from API 8.
160     */
161    result: string | Object | ArrayBuffer;
162    /**
163     * Server status code.
164     */
165    responseCode: ResponseCode | number;
166    /**
167     * All headers in the response from the server.
168     */
169    header: Object;
170    /**
171     * @since 8
172     */
173    cookies: string;
174  }
175}
176
177export default http;