1

I can make a post request to a REST api endpoint of a web service with curl successfully but couldnt do so with request module in node.js. Instead, I always get error CONNECTION ETIMEDOUT.What is the problem?

curl command:

curl -i --header "Content-Type: application/json" -XPOST 'http://<endpoint_url>/urls' -d '{
  "callback": "http://www.example.com/callback",
  "total": 3,
  "urls": [ {
     "url": "http://www.domain.com/index1.html"
     }, {
     "url": "http://www.domain.com/index2.html"
     }, {
     "url": "http://www.domain.com/index3.html"
     }
  ]
}'

code:

function sendRequestToEndPoint() {

  const sample = {
    "callback": "http://www.example.com/callback",
    "total": 3,
    "urls": [ {
      "url": "http://www.domain.com/index1.html"
      }, {
      "url": "http://www.domain.com/index2.html"
      }, {
      "url": "http://www.domain.com/index3.html"
      }
    ]
  }
  const options = {
    method: 'post',
    //headers: {
    //  'Content-Type': 'application/json',
    //  'Accept': 'application/json',
    //},
    url: 'http://<endpoint_url>/urls',
    json: sample
    //body: JSON.stringify(sample) // also tried this with headers on
  };

  console.log(sample);
  request(options, (error, response, body) => {

    console.log(response)
  });
}

Update: Turned out that it was because the api url I used is not correct.

shangsunset
  • 1,585
  • 4
  • 22
  • 38

2 Answers2

0

use querystring to stringify your json data,

var querystring = require('querystring');
...
sample = querystring.stringify(sample);

look at this answer How to make an HTTP POST request in node.js

Community
  • 1
  • 1
Slimane amiar
  • 934
  • 12
  • 27
  • hi, I tried using `querystring` on `sample` and it turned `sample` to `callback=http%3A%2F%2Fwww.example.com%2Fcallback&total=3&urls=&urls=&urls=`. and it didnt work. – shangsunset Aug 10 '16 at 15:19
  • plus, i think the endpoint is expecting `application/json`, so maybe theres no need for `querystring`? – shangsunset Aug 10 '16 at 15:28
0

this code works, you need to Stringify your json object using JSON.stringify , and use the methode write of the object request to send the sample json object

, http = require('http')
, bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
 extended: false
}));
var sample = JSON.stringify({
"callback": "http://www.example.com/callback"
, "total": 3
, "urls": [{
        "url": "http://www.domain.com/index1.html"
  }, {
        "url": "http://www.domain.com/index2.html"
  }, {
        "url": "http://www.domain.com/index3.html"
  }
 ]
 });
var options = {
hostname: 'localhost'
, port: 80
, path: '/test/a'
, method: 'POST'
, headers: {
    'Content-Type': 'application/json'
    , 'Content-Length': sample.length
 }
 };
 app.get('/', function (req, res) {
 var r = http.request(options, (response) => {
    console.log(`STATUS: ${response.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
    response.setEncoding('utf8');
    response.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
    });
    response.on('end', () => {
        console.log('No more data in response.');
    });
 });
 r.on('error', (e) => {
    console.log(`problem with request: ${e.message}`);
 });
 r.write(sample);
 r.end();
 res.send('ok');
});

a link for more details about http.request nodejs.org http.request(options[, callback])

Slimane amiar
  • 934
  • 12
  • 27