0

I am trying to call an external rest API from node server by using request node module.

let request = require('request');

var options = { 
  method: 'POST',
  url: 'https://somerestURI:3000',
  qs: { msg: 'some|data|for|other|server' } 
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

If I try to run the above code, query string value is being encoded to

some%7cdata%7cfor%7cother%7cserver

as a result I am not receiving correct response.

But if I fire the same request in POSTMAN. I am receiving the expected output(I think postman is not encoding query string).

So what I want is don't encode the query string value.

Any help would be greatly appreciated.

2 Answers 2

2

As answered here, you can disable encoding in qsStringifyOptions

var options = { 
   method: 'POST',
   url: 'https://somerestURI:3000',
   qs: { msg: 'some|data|for|other|server' },
   qsStringifyOptions: {
      encoding: false
   } 
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help...Added the edit you suggested but not working....when i dig into the request source code this is the line which is encoding query string value var qs = self._qs.stringify(base), where it is not checking for qsStringifyOptions.
after adding encoding: false url.parse in request module is encoding the query string
0

You can use node-rest-client package. It allows connecting to any REST API and get results as javascript Object.

var HttpClient = require('node-rest-client').Client;
var httpClient = new HttpClient();

// GET Call
httpClient.get("http://remote.site/rest/xml/method", function (data, response) {
       // parsed response body as js object 
       console.log(data);
       // raw response 
       console.log(response);
});)

or for POST Call

var args = {
     data: { test: "hello" },
     headers: { "Content-Type": "application/json" }
};
//POST Call
httpClient.post("http://remote.site/rest/xml/method", args, function (data, response) {
      // parsed response body as js object 
      console.log(data);
      // raw response 
      console.log(response);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.