Request is easy to use, however, I encountered a strange problem:
TypeError: The header content contains invalid characters
This is my code(very simple, just a GET request):
let request = require('request');
let url = "https://www.baidu.com/s?wd=整天就是背背背记记记,或者有些正,时间就这么浪费了";
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
} else {
console.log(error);
}
});
when I ran the code, the error occured. I hadn't set any headers in my request.
Request doesn't support the chinese characters? I am confused, anyone advice??
Since you are using a GET request, your URL path and parameters are members of the request headers. You should encode the parameter values like this:
let url = "https://www.baidu.com/s?wd=" +
encodeURIComponent("整天就是背背背记记记,或者有些正,时间就这么浪费了");
This will create an URL containing only ASCII characters:
https://www.baidu.com/s?wd=%E6%95%B4%E5%A4%A9%E5%B0%B1%E6%98%AF%E8%83%8C%E8%83%8C%E8%83%8C%E8%AE%B0%E8%AE%B0%E8%AE%B0%EF%BC%8C%E6%88%96%E8%80%85%E6%9C%89%E4%BA%9B%E6%AD%A3%EF%BC%8C%E6%97%B6%E9%97%B4%E5%B0%B1%E8%BF%99%E4%B9%88%E6%B5%AA%E8%B4%B9%E4%BA%86
Related
For some reason, I have a problem with "request" module.
var options = {
protocol: "https",
host: "euw.api.pvp.net",
pathname: "/api/lol/euw/v1.4/summoner/by-name/" + name,
search: "api_key=api-key-deleted"
};
var riotURL = url.format(options);
request(riotURL, function (error, response, body) {
if (!error && response.statusCode == 200) {
//doing someting
}
});
And then it goes weird. If name is simple, like "Dager", "hi mom" and so one, my app doing fine. But when I try to put names like "növentaynueve", "Mìnou" - my request return 404 response.
I managed to get full response text: http://pastebin.com/8Vra1i4X. And I can't understand what happened. All links in response are correct. But it looks like request module instead of sending request to
https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/n%C3%B6ventaynueve?api_key=api-key-deleted
sends request to
https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/n%EF%BF%BDventaynueve?api_key=api-key-deleted
But why?
Solved problem with encodeURI() function.
And funny thing: on Debian, it works even without encodeURI(). But on Windows 10 not. Both source files are UTF-8.
I'm trying to download and serve an image with Node.js, but when I get the image it ends with a plus sign:
[data]...9QAPvv70AXFAh2KLvv70/77+977+9
When I embed the data into an HTML image using data:image/jpg;base64, it displays a question mark (invalid image). I'm using this code:
request(image_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data_uri_prefix = 'data:' + response.headers['content-type'] + ';base64,';
var image = new Buffer(body).toString('base64');
// To ease testing for now
console.log(image);
}
});
I've tried including the 'binary' parameter when creating the Buffer, among other things. Additionally, the headers don't display any type of special encoding. I'm not really sure why the image is invalid. Is there something I'm missing in the process?
TLDR; solved, see comments below :)
I am really getting nuts with this: I want to post JSON to the Redmine API to send time entries imported via CSV files. Everything works well, until I try to send strings with German umlauts (ä ü ö) or special chars (ß).
I am using the request package with the following code:
var options = {
url: self.getHost() + path,
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
method: method
};
options.headers[self.getApiKeyHeader()] = self.getApiKey();
if (method !== HttpMethods.Get) {
params.time_entry.comments = 'äöüß';
options.body = JSON.stringify(params);
options.headers['Content-Length'] = options.body.length;
}
And send it like this:
request(options, function (error, response) {
if (response.statusCode === 201) {
console.log('Success');
}
else {
console.log('Error: ' + response.statusCode);
}
});
I always get an HTTP500 Error with server logs saying "Invalid byte sequence in UTF-8". I get the same error when I try to post the JSON via Postman. As my coworkers have no problems with Ruby and PHP script, I guess I got something terribly wrong in my script. Also tried the following alternatives for setting the options.body content:
options.body = new Buffer(JSON.stringify(params), encoding='utf8');
options.body = new Buffer(JSON.stringify(params, 'ascii').toString('utf8');
With both also not working.
I am using https://www.npmjs.com/package/request for the requests.
What am I doing wrong?
I'm experimenting with Node.js and web scraping. In this case, I'm trying to scrape the most recent songs from a local radio station for display. With this particular website, body returns nothing. When I try using google or any other website, body has a value.
Is this a feature of the website I'm trying to scrape?
Here's my code:
var request = require('request');
var url = "http://www.radiomilwaukee.org";
request(url, function(err,resp,body) {
if (!err && resp.statusCode == 200) {
console.log(body);
}
else
{
console.log(err);
}
});
That's weird, the website you're requesting doesn't seem to return anything unless the accept-encoding header is set to gzip. With that in mind, using this gist will work: https://gist.github.com/nickfishman/5515364
I ran the code within that gist, replacing the URL with "http://www.radiomilwaukee.org" and see the content within the sample.html file once the code has completed.
If you'd rather have access to the web page's content within the code, you could do something like this:
// ...
req.on('response', function(res) {
var body, encoding, unzipped;
if (res.statusCode !== 200) throw new Error('Status not 200');
encoding = res.headers['content-encoding'];
if (encoding == 'gzip') {
unzipped = res.pipe(zlib.createGunzip());
unzipped.on("readable", function() {
// collect the content in the body variable
body += unzipped.read().toString();
});
}
// ...
I am using this API to consume a ESPN API:
http://api.espn.com/v1/now?apikey=d4skkma8kt2ac8tqbusz38w6
In Node.js, using node-curl library, my snippet looks like this:
var Curl = require('node-curl/lib/Curl')
curl.setopt('URL', url);
curl.setopt('CONNECTTIMEOUT', 2);
curl.on('data', function(chunk) {
console.log(chunk);
});
But everytime when I run this code I keep on getting response as:
<h1>596 Service Not Found</h1>
Strange is, same URL if I hit from the browser I get the correct response so URL is not invalid. This happens only if I try to call from Node.js. Can anyone guide me how to resolve this error? I tried encoding/decoding url, still I get same response.
Also basically I am avoiding any vendor specific libraries as much as possible, since we should have generic api calling framework
You could use request, which is a very popular module. Here's an example of how you could do it:
var request = require('request');
var url = 'http://api.espn.com/v1/now?apikey=d4skkma8kt2ac8tqbusz38w6';
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(JSON.parse(body));
}
});