Object built-in object

Object HttpClient

httpClient object

httpThe client object simulates the browser environment to cache cookies, and accessesurlWhen carrying the corresponding cookie, differenthttpClient objects are isolated from each other, providinghttpThe 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

#lineWidth: 1.5 #font: Helvetica,sans-Serif #fontSize: 10 #leading: 1.6 #.this: fill=lightgray #.class: fill=white [<class>object|toString();toJSON()] [<this>HttpClient|new HttpClient()|cookies;timeout;enableCookie;autoRedirect;enableEncoding;maxBodySize;userAgent;poolSize;poolTimeout;proxyAgent;sslVerification|setClientCert();request();get();post();del();put();patch()] [object] <:- [HttpClient] HttpClient new HttpClient() cookies timeout enableCookie autoRedirect enableEncoding maxBodySize userAgent poolSize poolTimeout proxyAgent sslVerification setClientCert() request() get() post() del() put() patch() object toString() toJSON()

Constructor

HttpClient

HttpClient constructor, create a new HttpClient object

1
new HttpClient();

Member attributes

cookies

NArray, returnhttpClient HttpCookie Object list

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 http Browser ID in the 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, reference ssl VERIFY_* constants of the 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, Certificate, used to send to the server to verify the client
  • key: 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:


send http Request to the specified stream object and return the result

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

Call parameters:

Return result:


Request specified urlAnd 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, specify url, Must be the complete including the host url
  • 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 7 8
{ "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {}, "response_body": SeekableStream // 指定接受 resposne 数据的流 }

Where body,json, Pack must not appear at the same time. The default is {}, does not contain 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, specify url, Must be the complete including the host url
  • 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 7
{ "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Where body,json, Pack must not appear at the same time. The default is {}, does not contain 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, specify url, Must be the complete including the host url
  • 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 7
{ "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Where body,json, Pack must not appear at the same time. The default is {}, does not contain 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, specify url, Must be the complete including the host url
  • 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 7
{ "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Where body,json, Pack must not appear at the same time. The default is {}, does not contain 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, specify url, Must be the complete including the host url
  • 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 7
{ "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Where body,json, Pack must not appear at the same time. The default is {}, does not contain 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, specify url, Must be the complete including the host url
  • 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 7
{ "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Where body,json, Pack must not appear at the same time. The default is {}, does not contain 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