Request module return unicode characters - node.js

I'm trying to get normal response using request module in node.js
and i've got problem to get response from amazon.com as normal string
i dont know why, but ive got problem only with amazon.com (eg. amazon.it, amazon.co.uk does return normal string).
const request = require('request');
request.get(
{
uri: 'https://www.amazon.com',
encoding: 'utf-8'
},
function (error, response, body) {
console.log(body)
});
The code above return something like:
b��╝��W>�S�Uk��z�=8~r����9|r|P^?}p o╗��l���ߋ�t`ޜ^]��n!��
���U�>>�#�w-z�.��O�����Oo��������y�����g�N�/��{����_>���鳟�=s���w?�z��_W)i؃�
��;���2��9<�0ٷ8����<=�ϱ��ղ��3�=(�"�ԯ�; �3��=�8�2;=���28����#+,3��0"�+DZ �)�2�<�
���7�(W?�8�9\?�)#'���";�ķ���ܣ�ѽ����|�8 ��╚ ��'

The response returned by Amazon is gzipped. You have to provide the gzip option to your request.
const request = require('request');
request.get(
{
uri: 'https://www.amazon.com',
gzip: true,
},
function (error, response, body) {
console.log(body)
});

Related

Node JS request get original url

I'm sending GET requests like this in Node JS in a loop
request({
url : 'https://somelink.com',
method: 'GET'
},
function (error, response, body) {
console.log(response);
});
Since the response is async, is it possible to get the original request URL in the response?
Thanks!
You can get the original request href in the response.request object, like so:
const request = require("request");
request({
url : 'https://google.com',
method: 'GET'
},
function (error, response, body) {
if (!error) {
console.log("Original url:", response.request.uri.href);
console.log("Original uri object:", response.request.uri);
}
});
You can access more information in the request.uri object, for example:
console.log("Original uri:", response.request.uri);
This will give you some more useful information like port, path, host etc.

Send unparsed data in a Node JS request

I want to send a POST request (for example, with the 'request' module), but I don't find a way of sending unparsed data*.
*unparsed data => copied directly from the Chrome dev tool. Something like: tipo_accion=3&filtro=descarga&fecha=actual
It would also do the trick some way of translating that string to JSON.
I've tried so far with...
var request = require('request');
request.post({ url: 'https://target.com/form/', form: 'tipo_accion=3&filtro=descarga&fecha=actual' },
function (error, response, body) {
console.log(body)
}
);
... but it didn't work.
Firstly you should understand the difference between the request methods post and get.
The structure that you want to send:
tipo_accion=3&filtro=descarga&fecha=actual is telling me that you want to use a get request. So the proper code for that will be something like:
request(
'https://target.com/form/&tipo_accion=3&filtro=descarga&fecha=actual',
function (error, response, body) {
console.log(body)
},
);
But if it is a post request then you should use the json format
request.post({
url: 'https://target.com/form/', form: {
tipo_accion: 3,
filtro: 'descarga',
fecha: 'actual'
},
function(error, response, body) {
console.log(body)
}
);
You can convert form_data to string format and it works for me you can try it :
const request = require('request-promise');
var j = request.jar()
var data_fom = `a=value_a&b=value_b&c=value_c`
request({
url:"https://target.com/form/",
jar:j,
method:"POST",
resolveWithFullResponse: true,
form:data_form
})

Sending an API request with Nodejs

I've been trying to hit an API and get some data back from it (It's a free API not my own). So I've got my API token and I've had a look around and found that npm package request seems to be the best.
Within one of my routes I have,
request({
uri: "https://app.url-to-api:443/api/list-of-data",
method: "GET",
api_token: "my-api-token",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
console.log(body);
});
So I'm getting "message":"Authorization has been denied for this request." back which is obviously because my API Token isn't getting passed through.
This might be a stupid question, but where do I actually put the API token to validate my request?
Thanks!
In request it would be something like this:
request.get('http://some.server.com/', {
'auth': {
'bearer': 'bearerToken'
}
});
More details on what you can do with request are in the docs.
You have to pass api tokens in request headers please see the documentation for request
var request = require('request');
var options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'Access-Token': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback);

calling external rest api from node without encoding querystring params

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.
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
}
};
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);
});

Encoding issue with requesting JSON from StackOverflow API

I can't figure this out for the life of me. Below is an implementation with the request module, but I've also tried with the node-XMLHttpRequest module to no avail.
var request = require('request');
var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';
request.get({ url: url }, function(error, response, body) {
if (error || response.statusCode !== 200) {
console.log('There was a problem with the request');
return;
}
console.log(body); // outputs gibberish characters like �
console.log(body.toString()); // also outputs gibberish
});
Seems to be an encoding issue, but I've used the exact same code (with native XHR objects) in the browser and it works without problems. What am I doing wrong?
The content is gzipped. You can use request and zlib to decompress a streamed response from the API:
var request = require('request')
,zlib = require('zlib');
var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';
request({ url: url, headers: {'accept-encoding': 'gzip'}})
.pipe(zlib.createGunzip())
.pipe(process.stdout); // not gibberish
Further Reading: Easy HTTP requests with gzip/deflate compression
While pero's answer is correct, there's a simpler way to do this.
Since you're using request, you can also just add the gzip: true flag:
var request = require('request');
var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';
request.get({ url: url, headers: {'accept-encoding': 'gzip'}, gzip: true }, function(error, response, body) {
console.log(body); // not gibberish
});

Resources