I am trying to access a non utf-8 website using request module. Response is garbled for this request.
var request = require('request');
request('http://www.alc.co.jp/', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the web page.
}
});
Even after setting the encoding option to Shift_JIS I am seeing garbled Japanese text.
The request module is used to make HTTP calls. It is the simplest way of making HTTP calls in node.js using this request module. It follows redirects by default. It is easy to get started and easy to use. It is widely used and popular module for making HTTP calls. You can visit the link Install Request module.
The HTTP core module is a key module to Node.js networking. The module provides some properties and methods, and some classes. This property lists all the HTTP status codes and their description: Points to the global instance of the Agent object, which is an instance of the http.Agent class.
The fs module provides a lot of very useful functionality to access and interact with the file system. There is no need to install it. Being part of the Node.js core, it can be used by simply requiring it: Once you do so, you have access to all its methods, which include:
You need to do the conversion yourself. The example code below uses node-iconv.
var Iconv = require('iconv').Iconv;
var request = require('request');
request({
uri: 'http://www.jalan.net/',
encoding: null,
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
body = new Iconv('shift_jis', 'utf-8').convert(body).toString();
console.log(body); // Print the web page.
}
});
encoding: null
parameter asks request
not to convert the Buffer
(a byte array) into String
yet.Iconv
for converting into another Buffer
of UTF-8 encoding.Buffer
is good for being converted into a String.(BTW, http://www.alc.co.jp has switched to UTF-8, so I substituted with another site.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With