Object built-in object

Object HttpClient

http client object

http client object simulates the browser environment to cache cookies and carry the corresponding cookie when accessing the url . Different http client objects are isolated from each other and provide methods such as http request, get, and post. 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, create a new HttpClient object

1
new HttpClient();

Member attributes

cookies

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

1
readonly NArray HttpClient.cookies;

timeout

Integer, query and set the timeout unit in milliseconds

1
Integer HttpClient.timeout;

enableCookie

Boolean, cookie function switch, enabled by default

1
Boolean HttpClient.enableCookie;

autoRedirect

Boolean, automatic redirection 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 body size, in MB, the default is -1, unlimited size

1
Integer HttpClient.maxBodySize;

userAgent

String, query and set browser ID in http request

1
String HttpClient.userAgent;

poolSize

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

1
Integer HttpClient.poolSize;

poolTimeout

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

1
Integer HttpClient.poolTimeout;

proxyAgent

String, query and set proxy server

1
String HttpClient.proxyAgent;

Member function

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:

Returns the 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, specifies the http request method: GET, POST, etc.
  • url : String, the specified url , must be the full url containing the host
  • opts : Object, specify additional information

Returns the result:

opts contains the requested additional options, supported as follows:

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

Where body and json must not appear at the same time. Defaults to {}, 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 full url containing the host
  • opts : Object, specify additional information

Returns the result:

opts contains the requested additional options, supported as follows:

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

Where body and json must not appear at the same time. Defaults to {}, 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 full url containing the host
  • opts : Object, specify additional information

Returns the result:

opts contains the requested additional options, supported as follows:

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

Where body and json must not appear at the same time. Defaults to {}, 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 full url containing the host
  • opts : Object, specify additional information

Returns the result:

opts contains the requested additional options, supported as follows:

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

Where body and json must not appear at the same time. Defaults to {}, 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 full url containing the host
  • opts : Object, specify additional information

Returns the result:

opts contains the requested additional options, supported as follows:

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

Where body and json must not appear at the same time. Defaults to {}, 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 full url containing the host
  • opts : Object, specify additional information

Returns the result:

opts contains the requested additional options, supported as follows:

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

Where body and json must not appear at the same time. Defaults to {}, without any additional information


toString

Returns a string representation of the object, usually "[Native Object]". Objects can be reimplemented according to their own characteristics

1
String HttpClient.toString();

Returns the result:

  • String , returns a string representation of the object

toJSON

Returns a JSON representation of the object, typically returning a collection of readable properties defined by the object

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

Call parameters:

  • key : String, unused

Returns the result:

  • Value , returns a value containing a JSON serializable