title: HttpClient

Countless services rely on the HTTP-based communication nowadays, and it is a very common application scenario that web applications call back-end HTTP services.

The framework built in HttpClient based on urllib, you can quickly complete any HTTP request.

Using HttpClient by app

HttpClient will initialize to app.httpclient automatically during the application’s initialization.
Also added an method app.curl(url, options), which is equivalent to the app.httpclient.request(url, options).

So you can easily use app.curl to complete a HTTP request.

  1. // app.js
  2. module.exports = app => {
  3. app.beforeStart(async () => {
  4. // example: read the version info on https://registry.npm.taobao.org/egg/latest when it starts
  5. const result = await app.curl('https://registry.npm.taobao.org/egg/latest', {
  6. dataType: 'json',
  7. });
  8. app.logger.info('Egg latest version: %s', result.data.version);
  9. });
  10. };

Using HttpClient by Context

Framework also provides ctx.curl(url, options) and ctx.httpclient in Context, same as app.
So it’s very easy to use ctx.curl() to complete a HTTP request in the Context (such as in the controller)

  1. // app/controller/npm.js
  2. class NpmController extends Controller {
  3. async index() {
  4. const ctx = this.ctx;
  5. // example: request a npm module's info
  6. const result = await ctx.curl('https://registry.npm.taobao.org/egg/latest', {
  7. // parse JSON response
  8. dataType: 'json',
  9. // timeout of 3s
  10. timeout: 3000,
  11. });
  12. ctx.body = {
  13. status: result.status,
  14. headers: result.headers,
  15. package: result.data,
  16. };
  17. }
  18. }

Basic HTTP Request

HTTP has been widely used and have several methods to make request, but the methods are similar. We start with the basic four request methods then move to some more complex scenario.

In the following example, we will complete the request of https://httpbin.org in the controller.

GET

Reading data almost uses GET request. It is the most common type and widely used in the world of HTTP. And it is also easier to construct a request parameter.

  1. // app/controller/npm.js
  2. class NpmController extends Controller {
  3. async get() {
  4. const ctx = this.ctx;
  5. const result = await ctx.curl('https://httpbin.org/get?foo=bar');
  6. ctx.status = result.status;
  7. ctx.set(result.headers);
  8. ctx.body = result.data;
  9. }
  10. }
  • GET request might not need to set options.method. HttpClient Defalut method is set to GET
  • Return result will contains 3 attributes: status, headers and data
    • status: response status,for example 200, 302, 404, 500 and etc
    • headers: response header,similar to { 'content-type': 'text/html', ... }
    • data: response body,default HttpClient doesn’t do anything and returns as Buffer directly.
      Once the options.dataType is set,HttpClient will process the data based on the parameters

For the complete request parameter options and return value result, refer to below section options Parameters in Detail

POST

The scenario of creating data generally uses the POST request with body parameter, one more parameter compared to GET.

Take sending JSON boy as example:

  1. // app/controller/npm.js
  2. class NpmController extends Controller {
  3. async post() {
  4. const ctx = this.ctx;
  5. const result = await ctx.curl('https://httpbin.org/post', {
  6. // method is required
  7. method: 'POST',
  8. // telling HttpClient to send data as JSON by contentType
  9. contentType: 'json',
  10. data: {
  11. hello: 'world',
  12. now: Date.now(),
  13. },
  14. // telling HttpClient to process the return body as JSON format explicitly
  15. dataType: 'json',
  16. });
  17. ctx.body = result.data;
  18. }
  19. }

The following will explain POST to achieve Form function of form submission and file upload in detail.

PUT

Similar to POST, but PUT is better for data updating and replacement. Almost the same parameters as POST except setting method as PUT.

  1. // app/controller/npm.js
  2. class NpmController extends Controller {
  3. async put() {
  4. const ctx = this.ctx;
  5. const result = await ctx.curl('https://httpbin.org/put', {
  6. // method is required
  7. method: 'PUT',
  8. // telling HttpClient to send data as JSON by contentType
  9. contentType: 'json',
  10. data: {
  11. update: 'foo bar',
  12. },
  13. // telling HttpClient to process the return body as JSON format explicitly
  14. dataType: 'json',
  15. });
  16. ctx.body = result.data;
  17. }
  18. }

DELETE

DELETE request is to delete the data, request body don’t need to add request body but HttpClient don’t have the limitation.

  1. // app/controller/npm.js
  2. class NpmController extends Controller {
  3. async del() {
  4. const ctx = this.ctx;
  5. const result = await ctx.curl('https://httpbin.org/delete', {
  6. // method is required
  7. method: 'DELETE',
  8. // telling HttpClient to process the return body as JSON format explicitly
  9. dataType: 'json',
  10. });
  11. ctx.body = result.data;
  12. }
  13. }

Advanced HTTP request

In some real application scenarios, still have some more complex HTTP requests.

Form Submission

Interfaces of Browser-Oriented Form Submission (without files), usually require content-type: application/x-www-form-urlencoded for the data requesting.

  1. // app/controller/npm.js
  2. class NpmController extends Controller {
  3. async submit() {
  4. const ctx = this.ctx;
  5. const result = await ctx.curl('https://httpbin.org/post', {
  6. // method is required, supports POST,PUT and DELETE
  7. method: 'POST',
  8. // contentType is not needed, by default HttpClient will send request in application/x-www-form-urlencoded
  9. data: {
  10. now: Date.now(),
  11. foo: 'bar',
  12. },
  13. // telling HttpClient to process the return body as JSON format explicitly
  14. dataType: 'json',
  15. });
  16. ctx.body = result.data.form;
  17. // final response will similar as below:
  18. // {
  19. // "foo": "bar",
  20. // "now": "1483864184348"
  21. // }
  22. }
  23. }

Uploading Files by Multipart

Once form submission contains files, submission of requesting data must be multipart/form-data
We need to introduce third party module formstream to generate form objects that can be consumed by HttpClient.

  1. // app/controller/npm.js
  2. const FormStream = require('formstream');
  3. class NpmController extends Controller {
  4. async upload() {
  5. const ctx = this.ctx;
  6. const form = new FormStream();
  7. // set normal field and value
  8. form.field('foo', 'bar');
  9. // uploading the current file for test propose
  10. form.file('file', __filename);
  11. const result = await ctx.curl('https://httpbin.org/post', {
  12. // method is required, supports POST,PUT
  13. method: 'POST',
  14. // generate request headers following the requirements of multipart/form-data
  15. headers: form.headers(),
  16. // submitted as stream mode
  17. stream: form,
  18. // telling HttpClient to process the return body as JSON format explicitly
  19. dataType: 'json',
  20. });
  21. ctx.body = result.data.files;
  22. // final response will similar as below:
  23. // {
  24. // "file": "'use strict';\n\nconst For...."
  25. // }
  26. }
  27. }

Of course, you can add more files to achieve the requirements of upload multiple files at one time by form.file()

  1. form.file('file1', file1);
  2. form.file('file2', file2);

Uploading files in Stream Mode

In fact, Stream is the leading in the world of Node.js.
If the server supports streaming, the most friendly way is to send the Stream directly. Actually, Stream will be sent in Transfer-Encoding: chunked transmission coding format, which is implemented by HTTP module automatically.

  1. // app/controller/npm.js
  2. const fs = require('fs');
  3. const FormStream = require('formstream');
  4. class NpmController extends Controller {
  5. async uploadByStream() {
  6. const ctx = this.ctx;
  7. // uploading the current file for test propose
  8. const fileStream = fs.createReadStream(__filename);
  9. // httpbin.org not support stream mode, use the local stream interface instead
  10. const url = `${ctx.protocol}://${ctx.host}/stream`;
  11. const result = await ctx.curl(url, {
  12. // method is required, supports POST,PUT
  13. method: 'POST',
  14. // submitted by stream mode
  15. stream: fileStream,
  16. });
  17. ctx.status = result.status;
  18. ctx.set(result.headers);
  19. ctx.body = result.data;
  20. // final response will similar as below:
  21. // {"streamSize":574}
  22. }
  23. }

options Parameters in Detail

Due to the complexity of HTTP Request, the options parameters of httpclient.request(url, options) quite large. The actual usage of each optional parameter will be shown with descriptions and coding as below.

Default HttpClient Global Configuration

  1. // config/config.default.js
  2. exports.httpclient = {
  3. // whether to enable local DNS cache, default disable, enable will have two characteristics
  4. // 1. All DNS lookup will prefer to use the cache by default, even DNS query error does not affects the application
  5. // 2. For the same hostname, query only once during the interval of dnsCacheLookupInterval (default 10s)
  6. enableDNSCache: false,
  7. // minimum interval of DNS query on the same hostname
  8. dnsCacheLookupInterval: 10000,
  9. // maximum number of hostname DNS cache simultaneously, default 1000
  10. dnsCacheMaxLength: 1000,
  11. request: {
  12. // default timeout of request
  13. timeout: 3000,
  14. },
  15. httpAgent: {
  16. // default enable http KeepAlive
  17. keepAlive: true,
  18. // idle KeepAlive socket can survive for 4 seconds
  19. freeSocketKeepAliveTimeout: 4000,
  20. // when sockets have no activity for more than 30s, it will be processed as timeout
  21. timeout: 30000,
  22. // maximum number of sockets allow to be created
  23. maxSockets: Number.MAX_SAFE_INTEGER,
  24. // maximum number of idle sockets
  25. maxFreeSockets: 256,
  26. },
  27. httpsAgent: {
  28. // default enable https KeepAlive
  29. keepAlive: true,
  30. // idle KeepAlive socket can survive for 4 seconds
  31. freeSocketKeepAliveTimeout: 4000,
  32. // when sockets have no activity for more than 30s, it will be processed as timeout
  33. timeout: 30000,
  34. // maximum number of sockets allow to be created
  35. maxSockets: Number.MAX_SAFE_INTEGER,
  36. // maximum number of idle sockets
  37. maxFreeSockets: 256,
  38. },
  39. };

Application can overrides the configuration by config/config.default.js

data: Object

The request data will select the correct processing method automatically based on the method.

  • GET,HEAD: processed by querystring.stringify(data) then append to the query parameters of url.
  • POST,PUT, DELETE and etc: further judgments and process according to contentType.
    • contentType = json: processed by JSON.stringify(data) and set it as body before sending.
    • others: processed by querystring.stringify(data) and set it as body before sending
  1. // GET + data
  2. ctx.curl(url, {
  3. data: { foo: 'bar' },
  4. });
  5. // POST + data
  6. ctx.curl(url, {
  7. method: 'POST',
  8. data: { foo: 'bar' },
  9. });
  10. // POST + JSON + data
  11. ctx.curl(url, {
  12. method: 'POST',
  13. contentType: 'json',
  14. data: { foo: 'bar' },
  15. });

dataAsQueryString: Boolean

Once dataAsQueryString=true is set, even under POST, it will forces options.data to be processed by querystring.stringify then append to the url query parameters

The application scenarios that sending data using stream and pass additional request parameters by url query can be well resolved.

  1. ctx.curl(url, {
  2. method: 'POST',
  3. dataAsQueryString: true,
  4. data: {
  5. // generally it would be some validation parameters such as access token, etc.
  6. accessToken: 'some access token value',
  7. },
  8. stream: myFileStream,
  9. });

content: String|Buffer

Set request Context, if the parameter is set, it will ignore the data parameters

  1. ctx.curl(url, {
  2. method: 'POST',
  3. // Sending the raw xml data without HttpClient's to do processing
  4. content: '<xml><hello>world</hello></xml>',
  5. headers: {
  6. 'content-type': 'text/html',
  7. },
  8. });

stream: ReadStream

Set request context’s readable stream, default null.
If the parameter is set , HttpClient will ignore data and content

  1. ctx.curl(url, {
  2. method: 'POST',
  3. stream: fs.createReadStream('/path/to/read'),
  4. });

writeStream: WriteStream

Set receive response data’s writeable stream, default null.
Once the parameter is set, response result.data is set to null because all data are written to writeStream.

  1. ctx.curl(url, {
  2. writeStream: fs.createWriteStream('/path/to/store'),
  3. });

consumeWriteStream: Boolean

Whether to wait for writeStream completely finished as the response well received
This parameter is not recommended to modify the default value, unless we know it’s side effect are acceptable. Otherwise, the writeStream data is likely to be incomplete.

method: String

Set request method, default GET. Support all GET、POST、PUT、DELETE、PATCH and so on all HTTP methods

contentType: String

Set request data format ,default undefined,HttpClient will sets automatically based on the data and content parameters.
When data is object, the default setting would be form. Support json format.

If need to send data by JSON

  1. ctx.curl(url, {
  2. method: 'POST',
  3. data: {
  4. foo: 'bar',
  5. now: Date.now(),
  6. },
  7. contentType: 'json',
  8. });

dataType: String

Set the response data format, default return the raw buffer formatted data without processing. Support text and json

Note: If json is set,a JSONResponseFormatError error would be thrown if fails to parse the response data.

  1. const jsonResult = await ctx.curl(url, {
  2. dataType: 'json',
  3. });
  4. console.log(jsonResult.data);
  5. const htmlResult = await ctx.curl(url, {
  6. dataType: 'text',
  7. });
  8. console.log(htmlResult.data);

fixJSONCtlChars: Boolean

Whether filter the special control characters in the response data (U+0000 ~ U+001F),default false
Typically, the JSON data returned by some CGI system might contains such special control characters, which can be filter automatically by setting the parameters.

  1. ctx.curl(url, {
  2. fixJSONCtlChars: true,
  3. dataType: 'json',
  4. });

headers: Object

Custom request headers

  1. ctx.curl(url, {
  2. headers: {
  3. 'x-foo': 'bar',
  4. },
  5. });

timeout: Number|Array

Timeout of request, default [ 5000, 5000 ], timeout of connection creation is 5s, and the timeout of receive response is 5s.

  1. ctx.curl(url, {
  2. // 3s timeout of connection creation, and the 3s timeout of receive response
  3. timeout: 3000,
  4. });
  5. ctx.curl(url, {
  6. // 1s timeout of connection creation, and the 30s timeout of receive response for the responsing of larger scenarios
  7. timeout: [ 1000, 30000 ],
  8. });

agent: HttpAgent

Allows to override the default HttpAgent through this parameter. If you don’t want to enable KeepAlive, set this parameter to false.

  1. ctx.curl(url, {
  2. agent: false,
  3. });

httpsAgent: HttpsAgent

Allows to override the default HttpsAgent through this parameter. If you don’t want to enable KeepAlive, set this parameter to false.

  1. ctx.curl(url, {
  2. httpsAgent: false,
  3. });

auth: String

Parameter of Simple login authorization (Basic Authentication), will send the login information to the Authorization request in clear form.

  1. ctx.curl(url, {
  2. // parameter must follow the format of `user:password`
  3. auth: 'foo:bar',
  4. });

digestAuth: String

Parameter of the Digest Authentication. If the parameter is set, it will attempt to generate the Authorization request header for the 401 response automatically then try requesting for authorization once.

  1. ctx.curl(url, {
  2. // parameter must follow the format of `user:password`
  3. digestAuth: 'foo:bar',
  4. });

followRedirect: Boolean

Whether to follow 3xx redirect response, default false

  1. ctx.curl(url, {
  2. followRedirect: true,
  3. });

maxRedirects: Number

Set the maximum number of automatic redirects to prevent the endless redirect loop, default 10 times.
The parameter should not be set too large and only works in the followRedirect=true

  1. ctx.curl(url, {
  2. followRedirect: true,
  3. // maximum allowed redirect 5 times
  4. maxRedirects: 5,
  5. });

formatRedirectUrl: Function(from, to)

formatRedirectUrl allow us to customize the implementation of 302、301 other redirect URL splicing, default url.resolve (from, to).

  1. ctx.curl(url, {
  2. formatRedirectUrl: (from, to) => {
  3. // for example you can correct the redirection of wrong url here
  4. if (to === '//foo/') {
  5. to = '/foo';
  6. }
  7. return url.resolve(from, to);
  8. },
  9. });

beforeRequest: Function(options)

HttpClient will attempt to invoke the beforeRequest hook before requesting officially, allowing us to make the last modification of the request parameter here.

  1. ctx.curl(url, {
  2. beforeRequest: options => {
  3. // For example, we can set the global request ID to facilitate log tracking
  4. options.headers['x-request-id'] = uuid.v1();
  5. },
  6. });

streaming: Boolean

Whether to return the response stream directly, default false
After enable streaming, HttpClient will return immediately after getting the response object res,
At this moment result.headers and result.status can be read, but still cannot read the data

  1. const result = await ctx.curl(url, {
  2. streaming: true,
  3. });
  4. console.log(result.status, result.data);
  5. // result.res is a ReadStream Object
  6. ctx.body = result.res;

if res is not passed to body directly, then we must consume this stream and do well in error handling.

gzip: Boolean

Whether to support gzip response format, default false
After enable gzip, HttpClient will set Accept-Encoding: gzip header and extract the data with Content-Encoding: gzip response header automatically.

  1. ctx.curl(url, {
  2. gzip: true,
  3. });

timing: Boolean

Whether to enable the time measurement for each phase, default false
After enable the timing, you can get the time measurements of HTTP request (in milliseconds) from the result.res.timing.
Through these measurements, we can easily locate the slowest environment in the request, similar to the Chrome network timing.

Measurement timing’s analysis of each stage:

  • queuing: allocating socket time consuming
  • dnslookup: DNS queries time consuming
  • connected: socket three handshake success time consuming
  • requestSent: requesting full data time consuming
  • waiting: first byte to received response time consuming
  • contentDownload: full response data time consuming
  1. const result = await ctx.curl(url, {
  2. timing: true,
  3. });
  4. console.log(result.res.timing);
  5. // {
  6. // "queuing":29,
  7. // "dnslookup":37,
  8. // "connected":370,
  9. // "requestSent":1001,
  10. // "waiting":1833,
  11. // "contentDownload":3416
  12. // }

ca,rejectUnauthorized,pfx,key,cert,passphrase,ciphers,secureProtocol

These are parameters are passed to the HTTPS modules,details refer to https.request(options, callback)

Debugging Aid

Framework provides egg-development-proxyagent plugin to help developers to debug.

Install and enable pulgin:

  1. $ npm i egg-development-proxyagent --save
  1. // config/plugin.js
  2. exports.proxyagent = {
  3. enable: true,
  4. package: 'egg-development-proxyagent',
  5. }

Open capture tools, we can use charles or fiddler, here we take to anyproxy demonstrate

  1. $ npm install anyproxy -g
  2. $ anyproxy --port 8888

Starting application using environment variables:

  1. $ http_proxy=http://127.0.0.1:8888 npm run dev

Then it works correctly, and all requests that go through HttpClient can be viewed in the consle of http://localhost:8002.

anyproxy

Note: the pulgin only start in local environments by defalut

Known issues

Connection Timeout

  • Exception: ConnectionTimeoutError
  • Scene: usually occurred by the DNS query is slow, or the network is slow between the client and server
  • Troubleshooting Suggestion: increase the timeout parameter appropriately.

Service Response Timeout

  • Exception: ResponseTimeoutError
  • Scene: usually occurred by network is slower between the client and server, and happens when the data is relatively large.
  • Troubleshooting Suggestion: increase the timeout parameter appropriately.

Service Disconnect

  • Exception: ResponseError, code: ECONNRESET
  • Scene: usually the server actively disconnects the socket connection, causing the HTTP request link exceptions.
  • Troubleshooting Suggestion: please check if server has network exception at that time

Service is unreachable

  • Exception: RequestError, code: ECONNREFUSED, status: -1
  • Scene: usually because the requested URL which attached IP or the port cannot connect successfully.
  • Troubleshooting Suggestion: make sure the IP or port is set correctly

Domain name is not existing

  • Exception: RequestError, code: ENOTFOUND, status: -1
  • Scene: usually the domain name requested by URL cannot be resolved by DNS successfully.
  • Troubleshooting Suggestion: make sure the domain name exists, and also check to see if the DNS service is properly configured.

JSON Response data format error

  • Exception: JSONResponseFormatError
  • scene: the dataType=json is set and this exception is thrown in response data that does not match JSON format.
  • Troubleshooting Suggestion: make sure that the server no matter what situations are returns the data in JSON format correctly.

Global request and response events

In enterprise application scenarios, generally a unified tracer log is needed.
To facilitate monitoring HttpClient requests and responses on the app level, we agreed on global request and response to expose these two events.

  1. init options
  2. |
  3. V
  4. emit `request` event
  5. |
  6. V
  7. send request and receive response
  8. |
  9. V
  10. emit `response` event
  11. |
  12. V
  13. end

request event occurs before the network operation

A request event is triggered before the request is sent, allowing blocking of the request.

  1. app.httpclient.on('request', req => {
  2. req.url //request url
  3. req.ctx //context of the request
  4. // you can set some trace headers here for full link tracking propose
  5. });

response event occurs after the end of network operation

After the end of request, a response event is triggered, so that the external event can be subscribed to the log printing.

  1. app.httpclient.on('response', result => {
  2. result.res.status
  3. result.ctx //context of the request
  4. result.req //the corresponding req object, which the req in the request event
  5. });

Example

Full examples can be found on eggjs/exmaples/httpclient .