Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js mikeal/request module - Garbled non-utf8 website (Shift_JIS)

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.

like image 321
user2310817 Avatar asked Sep 02 '14 12:09

user2310817


People also ask

How to make HTTP calls using request module in Node JS?

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.

What is http core in Node JS?

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.

What is the fs module in Node JS?

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:


1 Answers

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.
      }
    });
  1. The encoding: null parameter asks request not to convert the Buffer (a byte array) into String yet.
  2. We pass this buffer to Iconv for converting into another Buffer of UTF-8 encoding.
  3. Now this 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.)

like image 88
Alan Tam Avatar answered Jan 02 '23 12:01

Alan Tam