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

I am trying to access the non utf-8 website using the request module. The response to this request is garbled.

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 Shift_JIS encoding option, I see distorted Japanese text.

+5
source share
1 answer

You need to do the conversion yourself. The following code example 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. } }); 
  • The encoding: null parameter requests request not to convert the Buffer (array of bytes) to String .
  • We pass this buffer to Iconv for conversion to another UTF-8 encoding buffer.
  • Now this Buffer is good for converting to string.

(BTW, http://www.alc.co.jp switched to UTF-8, so I replaced another site.)

+4
source

Source: https://habr.com/ru/post/1201640/


All Articles