Object built-in object

ObjectHttpClient

HttpClient is a class library designed for HTTP client functions, providing basic HTTP/HTTPS requests, proxy access, cookie management and other functions

You can easily access and operate web pages using HttpClient. Here is a simple example - printing out the source code on a web page:

1 2 3 4 5
const http = require('http'); const res = http.get('http://www.example.com/'); console.log(res.body.readAll().toString());

In this example, introduced through requirehttpmodule and then usehttp.getInitiate a get request, whereurlThe parameter specifies the requested URL. becausehttp.getThe method returns aHttpResponseObject, so you can access the body content returned by the request through its body property and convert it into a string through the toString method.

when requestedurlIt is https type instead ofhttptype, the code only needs to changehttpJust change it to https:

1 2 3 4 5
const http = require('http'); const res = http.get('https://www.example.com/'); console.log(res.body.readAll().toString());

In addition, there are examples of directly initiating POST requests and setting User-Agent through HttpClient:

1 2 3 4 5 6 7 8 9 10 11
const http = require('http'); const httpClient = new http.Client(); httpClient.userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'; const res = httpClient.post('http://www.example.com/post', { json: { name: 'fibjs', version: '0.31.0' } }); console.log(res.body.readAll().toString());

In this example, an HttpClient object httpClient is first created, and its userAgent is set to the browser's User-Agent. Then initiate a post request through its post method, in which the parameters name and version specify the main content of the request. Finally, the main content of the return value is output.

inheritance relationship

Constructor

HttpClient

HttpClient constructor, creates a new HttpClient object

1
new HttpClient();

member properties

cookies

NArray, returnhttpclientHttpCookieobject list

1
readonly NArray HttpClient.cookies;

timeout

Integer, query and set timeout unit 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;

maxHeadersCount

Integer, query and set the maximum number of request headers, the default is 128

1
Integer HttpClient.maxHeadersCount;

maxHeaderSize

Integer, query and set the maximum request header length, the default is 8192

1
Integer HttpClient.maxHeaderSize;

maxBodySize

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

1
Integer HttpClient.maxBodySize;

userAgent

String, query and sethttpThe browser identifier in the 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, default 10000 ms

1
Integer HttpClient.poolTimeout;

http_proxy

String, query and sethttpRequest agent, supporthttp/https/socks5 proxy

1
String HttpClient.http_proxy;

https_proxy

String, query and set https request proxy, supporthttp/https/socks5 proxy, if not set, or set to empty, http_proxy will be reused.

1
String HttpClient.https_proxy;

sslVerification

Integer, query and set the certificate verification mode when connecting to https, referencesslVERIFY_* constants of the module, the default value isssl.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

sendhttpRequest the specified stream object and return the result

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

Call parameters:

Return results:


sendhttpRequest 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 results:


request specifiedurl, and return the result

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

Call parameters:

  • method: String, specifyhttpRequest method: GET, POST, etc.
  • url: String, specifyurl, must be the complete containing hosturl
  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {}, "response_body": SeekableStream // specify the response.body stream }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


Use the GET method to request the specifiedurl, and returns the result, which is equivalent to request("GET", ...)

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

Call parameters:

  • url: String, specifyurl, must be the complete containing hosturl
  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


Request options specifiedurl, and return the result

1
HttpResponse HttpClient.request(Object opts) async;

Call parameters:

  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


get

Use the GET method to request the specifiedurl, and returns the result, which is equivalent to request("GET", ...)

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

Call parameters:

  • url: String, specifyurl, must be the complete containing hosturl
  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


post

Use the POST method to request the specifiedurl, and returns the result, which is equivalent to request("POST", ...)

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

Call parameters:

  • url: String, specifyurl, must be the complete containing hosturl
  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


del

Use the DELETE method to request the specifiedurl, and returns the result, which is equivalent to request("DELETE", ...)

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

Call parameters:

  • url: String, specifyurl, must be the complete containing hosturl
  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


put

Use the PUT method to request the specifiedurl, and returns the result, which is equivalent to request("PUT", ...)

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

Call parameters:

  • url: String, specifyurl, must be the complete containing hosturl
  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


patch

Use the PATCH method to request the specifiedurl, and returns the result, which is equivalent to request("PATCH", ...)

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

Call parameters:

  • url: String, specifyurl, must be the complete containing hosturl
  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


Use the HEAD method to request the specifiedurl, and returns the result, which is equivalent to request("PATCH", ...)

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

Call parameters:

  • url: String, specifyurl, must be the complete containing hosturl
  • opts: Object, specify additional information

Return results:

opts contains additional options for the request, supported as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "method": "GET", // specify the http request method: GET, POST, etc, default: GET. "protocol": "http", "slashes": true, "username": "", "password": "", "hostname": "", "port": "", "pathname": "", "query": {}, "body": SeekableStream | Buffer | String | {}, "json": {}, "pack": {}, "headers": {} }

Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information


toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String HttpClient.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

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

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable