node-modules / agentkeepalive

Support keepalive http agent.
MIT License
579 stars 57 forks source link

How to write a POST request? #68

Closed michealChin closed 6 years ago

michealChin commented 6 years ago

The following example shows a Get request, I would like to know how to execute a POST request?

const options = {
  host: 'cnodejs.org',
  port: 80,
  path: '/',
  method: 'GET',
  agent: keepaliveAgent,
};

const req = http.request(options, res => {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});
req.on('error', e => {
  console.log('problem with request: ' + e.message);
});
req.end();
denghongcai commented 6 years ago

agentkeepalive is just an alternative to httpAgent

About How to Send A Request, please refer to https://nodejs.org/api/http.html#http_http_request_options_callback

michealChin commented 6 years ago

Hi,

I got it, Thanks!