Object built-in object

Object HttpClient

http client object

http client object simulates the browser environment to cache cookies and carries the corresponding cookie when accessing the url . Different http client objects are isolated from each other and provide http request, get, post and other methods. The usage is as follows:

1 2 3
var http = require('http'); var httpClient = new http.Client(); httpClient.request('GET', 'http://fibjs.org');

Inheritance

Constructor

HttpClient

HttpClient constructor to create a new HttpClient object

1
new HttpClient();

Member attributes

cookies

NArray, returns a list of HttpCookie objects of the http client

1
readonly NArray HttpClient.cookies;

timeout

Integer, query and set the timeout time in milliseconds

1
Integer HttpClient.timeout;

enableCookie

Boolean, cookie function switch, enabled by default

1
Boolean HttpClient.enableCookie;

autoRedirect

Boolean, automatic redirect function switch, enabled by default

1
Boolean HttpClient.autoRedirect;

enableEncoding

Boolean, automatic decompression function switch, enabled by default

1
Boolean HttpClient.enableEncoding;

maxBodySize

Integer, query and set the maximum size of the body, in MB, the default is -1, and the size is not limited

1
Integer HttpClient.maxBodySize;

userAgent

String, query and set the browser ID in the http request

1
String HttpClient.userAgent;

poolSize

Integer, query and set the maximum number of keep-alive cache connections, the default is 128

1
Integer HttpClient.poolSize;

poolTimeout

Integer, query and set the keep-alive cache connection timeout time, the default is 10000 ms

1
Integer HttpClient.poolTimeout;

proxyAgent

String, query and set proxy server

1
String HttpClient.proxyAgent;

sslVerification

Integer, query and set the certificate verification mode when connecting to https, refer to the VERIFY_* constant of the ssl module, the default value is ssl.verification

1
Integer HttpClient.sslVerification;

Member function

setClientCert

Set default client certificate

1 2
HttpClient.setClientCert(X509Cert crt, PKey key);

Call parameters:

  • crt : X509Cert , X509Cert certificate, used for client authentication server
  • key : PKey , PKey private key, used to talk to the client

request

Send http request to the specified stream object and return the result

1 2
HttpResponse HttpClient.request(Stream conn, HttpRequest req) async;

Call parameters:

Return result:


Request the specified url and return the result

1 2 3
HttpResponse HttpClient.request(String method, String url, Object opts = {}) async;

Call parameters:

  • method : String, specify http request method: GET, POST, etc.
  • url : String, the specified url , must be the complete url including the host
  • opts : Object, specify additional information

Return result:

opts contains additional options requested, and the supported content is as follows:

1 2 3 4 5 6
{ "query": {}, "body": SeekedStream | Buffer | String | {}, "json": {}, "headers": {} }

The body and json must not appear at the same time. The default is {} without any additional information


get

Use the GET method to request the specified url and return the result, which is equivalent to request("GET", ...)

1 2
HttpResponse HttpClient.get(String url, Object opts = {}) async;

Call parameters:

  • url : String, the specified url , must be the complete url including the host
  • opts : Object, specify additional information

Return result:

opts contains additional options requested, and the supported content is as follows:

1 2 3 4 5 6
{ "query": {}, "body": SeekedStream | Buffer | String | {}, "json": {}, "headers": {} }

The body and json must not appear at the same time. The default is {} without any additional information


post

Use the POST method to request the specified url and return the result, which is equivalent to request("POST", ...)

1 2
HttpResponse HttpClient.post(String url, Object opts = {}) async;

Call parameters:

  • url : String, the specified url , must be the complete url including the host
  • opts : Object, specify additional information

Return result:

opts contains additional options requested, and the supported content is as follows:

1 2 3 4 5 6
{ "query": {}, "body": SeekedStream | Buffer | String | {}, "json": {}, "headers": {} }

The body and json must not appear at the same time. The default is {} without any additional information


del

Use the DELETE method to request the specified url and return the result, which is equivalent to request("DELETE", ...)

1 2
HttpResponse HttpClient.del(String url, Object opts = {}) async;

Call parameters:

  • url : String, the specified url , must be the complete url including the host
  • opts : Object, specify additional information

Return result:

opts contains additional options requested, and the supported content is as follows:

1 2 3 4 5 6
{ "query": {}, "body": SeekedStream | Buffer | String | {}, "json": {}, "headers": {} }

The body and json must not appear at the same time. The default is {} without any additional information


put

Use the PUT method to request the specified url and return the result, which is equivalent to request("PUT", ...)

1 2
HttpResponse HttpClient.put(String url, Object opts = {}) async;

Call parameters:

  • url : String, the specified url , must be the complete url including the host
  • opts : Object, specify additional information

Return result:

opts contains additional options requested, and the supported content is as follows:

1 2 3 4 5 6
{ "query": {}, "body": SeekedStream | Buffer | String | {}, "json": {}, "headers": {} }

The body and json must not appear at the same time. The default is {} without any additional information


patch

Use the PATCH method to request the specified url and return the result, which is equivalent to request("PATCH", ...)

1 2
HttpResponse HttpClient.patch(String url, Object opts = {}) async;

Call parameters:

  • url : String, the specified url , must be the complete url including the host
  • opts : Object, specify additional information

Return result:

opts contains additional options requested, and the supported content is as follows:

1 2 3 4 5 6
{ "query": {}, "body": SeekedStream | Buffer | String | {}, "json": {}, "headers": {} }

The body and json must not appear at the same time. The default is {} without any additional information


toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be re-implemented according to its own characteristics

1
String HttpClient.toString();

Return result:

  • String , returns the string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

1
Value HttpClient.toJSON(String key = "");

Call parameters:

  • key : String, unused

Return result:

  • Value , returns a value containing JSON serializable