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
3var http = require('http');
var httpClient = new http.Client();
httpClient.request('GET', 'http://fibjs.org');
Inheritance
Constructor
HttpClient
HttpClient constructor, create a new HttpClient object
1new HttpClient();
Member attributes
cookies
NArray, returnhttpClient HttpCookie Object list
1readonly NArray HttpClient.cookies;
timeout
Integer, query and set the timeout time in milliseconds
1Integer HttpClient.timeout;
enableCookie
Boolean, cookie function switch, enabled by default
1Boolean HttpClient.enableCookie;
autoRedirect
Boolean, automatic redirect function switch, enabled by default
1Boolean HttpClient.autoRedirect;
enableEncoding
Boolean, automatic decompression function switch, enabled by default
1Boolean 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
1Integer HttpClient.maxBodySize;
userAgent
String, query and set http Browser ID in the request
1String HttpClient.userAgent;
poolSize
Integer, query and set the maximum number of keep-alive cache connections, the default is 128
1Integer HttpClient.poolSize;
poolTimeout
Integer, query and set the keep-alive cache connection timeout time, the default is 10000 ms
1Integer HttpClient.poolTimeout;
proxyAgent
String, query and set proxy server
1String 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
1Integer HttpClient.sslVerification;
Member function
setClientCert
Set default client certificate
1
2HttpClient.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
2HttpResponse HttpClient.request(Stream conn,
HttpRequest req) async;
Call parameters:
- conn: Stream, Specify the stream object to process the request
- req: HttpRequest, To be sent HttpRequest Object
Return result:
- HttpResponse, Return server response
send http Request to the specified stream object and return the result
1
2
3HttpResponse HttpClient.request(Stream conn,
HttpRequest req,
SeekableStream response_body) async;
Call parameters:
- conn: Stream, Specify the stream object to process the request
- req: HttpRequest, To be sent HttpRequest Object
- response_body: SeekableStream, Specify the flow of response.body
Return result:
- HttpResponse, Return server response
Request specified urlAnd return the result
1
2
3HttpResponse 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:
- HttpResponse, Return server response
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
2HttpResponse 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:
- HttpResponse, Return server response
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
2HttpResponse 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:
- HttpResponse, Return server response
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
2HttpResponse 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:
- HttpResponse, Return server response
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
2HttpResponse 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:
- HttpResponse, Return server response
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
2HttpResponse 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:
- HttpResponse, Return server response
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
1String 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
1Value HttpClient.toJSON(String key = "");
Call parameters:
- key: String, unused
Return result:
- Value, Returns a value containing JSON serializable