interface_sdk-js/api/@ohos.net.http.d.ts
clevercong 74797620d3 update telephony and net js api.
Signed-off-by: clevercong <lichunlin2@huawei.com>
2022-02-18 16:45:55 +08:00

152 lines
3.8 KiB
TypeScript

/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {AsyncCallback, Callback} from "./basic";
/**
* Provides http related APIs.
*
* @since 6
* @sysCap SystemCapability.Communication.NetStack
*/
declare namespace http {
/**
* Creates an HTTP request task.
*/
function createHttp(): HttpRequest;
export interface HttpRequestOptions {
/**
* Request method.
*/
method?: RequestMethod; // default is GET
/**
* Additional data of the request.
* extraData can be a string or an Object (API 6) or an ArrayBuffer(API 8).
*/
extraData?: string | Object | ArrayBuffer;
/**
* HTTP request header.
*/
header?: Object; // default is 'content-type': 'application/json'
/**
* Read timeout period. The default value is 60,000, in ms.
*/
readTimeout?: number; // default is 60s
/**
* Connection timeout interval. The default value is 60,000, in ms.
*/
connectTimeout?: number; // default is 60s.
}
export interface HttpRequest {
/**
* Initiates an HTTP request to a given URL.
*
* @param url URL for initiating an HTTP request.
* @param options Optional parameters {@link HttpRequestOptions}.
* @param callback Returns {@link HttpResponse}.
* @permission ohos.permission.INTERNET
*/
request(url: string, callback: AsyncCallback<HttpResponse>): void;
request(url: string, options: HttpRequestOptions, callback: AsyncCallback<HttpResponse>): void;
request(url: string, options?: HttpRequestOptions): Promise<HttpResponse>;
/**
* Destroys an HTTP request.
*/
destroy(): void;
/**
* Registers an observer for HTTP Response Header events.
*/
on(type: "headerReceive", callback: AsyncCallback<Object>): void;
/**
* Unregisters the observer for HTTP Response Header events.
*/
off(type: "headerReceive", callback?: AsyncCallback<Object>): void;
}
export enum RequestMethod {
OPTIONS = "OPTIONS",
GET = "GET",
HEAD = "HEAD",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
TRACE = "TRACE",
CONNECT = "CONNECT"
}
export enum ResponseCode {
OK = 200,
CREATED,
ACCEPTED,
NOT_AUTHORITATIVE,
NO_CONTENT,
RESET,
PARTIAL,
MULT_CHOICE = 300,
MOVED_PERM,
MOVED_TEMP,
SEE_OTHER,
NOT_MODIFIED,
USE_PROXY,
BAD_REQUEST = 400,
UNAUTHORIZED,
PAYMENT_REQUIRED,
FORBIDDEN,
NOT_FOUND,
BAD_METHOD,
NOT_ACCEPTABLE,
PROXY_AUTH,
CLIENT_TIMEOUT,
CONFLICT,
GONE,
LENGTH_REQUIRED,
PRECON_FAILED,
ENTITY_TOO_LARGE,
REQ_TOO_LONG,
UNSUPPORTED_TYPE,
INTERNAL_ERROR = 500,
NOT_IMPLEMENTED,
BAD_GATEWAY,
UNAVAILABLE,
GATEWAY_TIMEOUT,
VERSION
}
export interface HttpResponse {
/**
* result can be a string or an Object (API 6) or an ArrayBuffer(API 8).
*/
result: string | Object | ArrayBuffer;
/**
* Server status code.
*/
responseCode: ResponseCode | number;
/**
* All headers in the response from the server.
*/
header: Object;
/**
* @since 8
*/
cookies: string;
}
}
export default http;