Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs encoding using request

I am trying to get the correct encoding with request.

request.get({
    "uri":'http://www.bold.dk/tv/',
    "encoding": "text/html;charset='charset=utf-8'"
  },
  function(err, resp, body){    
    console.log(body);
  }
);

No matter what I do the encoding of the danish chars are not right.

Any thoughts?

like image 623
hippie Avatar asked Aug 20 '12 15:08

hippie


People also ask

How do I use HTTP request in node JS?

Setup a new project: To create a new project, enter the following command in your terminal. Project Structure: It will look like the following. Approach 1: In this approach we will send request to getting a resource using AXIOS library. Axios is a promise base HTTP client for NodeJS.

What is setHeader in node JS?

setHeader(name, value) (Added in v0. 4.0) method is an inbuilt application programming interface of the 'http' module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.

What is HTTP createServer?

The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.

What is req and res in node JS?

The req object represents the HTTP request and has properties for the request query string, parameters, body, and HTTP headers. The res object represents the HTTP response that an Express app sends when it gets an HTTP request. In our case, we are sending a text Hello World whenever a request is made to the route / .


2 Answers

You can use iconv (lite) to convert this. You also need to tell request not to actively set the encoding to the default of UTF-8 by setting the encoding property to null. Therefore you should do:

var iconv = require('iconv-lite');
request.get({
    uri:'http://www.bold.dk/tv/',
    encoding: null
  },
  function(err, resp, body){    
    var bodyWithCorrectEncoding = iconv.decode(body, 'iso-8859-1');
    console.log(bodyWithCorrectEncoding);
  }
);
like image 90
Jens Mikkelsen Avatar answered Oct 08 '22 04:10

Jens Mikkelsen


Maybe your trouble is in 'Accept-Encoding' header. Let's say you have Headers like 'Accept-Encoding': 'gzip,deflate'

If it's so, you have 2 ways to fixing this:

  1. Remove this Header
  2. Use the following code to unzip the data:

    const req = request(options, res => {
        let buffers = []
        let bufferLength = 0
        let strings = []
    
        const getData = chunk => {
            if (!Buffer.isBuffer(chunk)) {
                strings.push(chunk)
            } else if (chunk.length) {
                bufferLength += chunk.length
                buffers.push(chunk)
            }
        }
    
        const endData = () => {
            let response = {code: 200, body: ''}
            if (bufferLength) {
                response.body = Buffer.concat(buffers, bufferLength)
                if (options.encoding !== null) {
                    response.body = response.body.toString(options.encoding)
                }
                buffers = []
                bufferLength = 0
            } else if (strings.length) {
                if (options.encoding === 'utf8' && strings[0].length > 0 && strings[0][0] === '\uFEFF') {
                    strings[0] = strings[0].substring(1)
                }
                response.body = strings.join('')
            }
            console.log('response', response)
        };
    
        switch (res.headers['content-encoding']) {
            // or, just use zlib.createUnzip() to handle both cases
            case 'gzip':
                res.pipe(zlib.createGunzip())
                    .on('data', getData)
                    .on('end', endData)
                break;
            case 'deflate':
                res.pipe(zlib.createInflate())
                    .on('data', getData)
                    .on('end', endData)
                break;
            default:
                res.pipe(zlib.createInflate())
                    .on('data', getData)
                    .on('end', endData)
                break;
        }
    });
    
like image 34
woolfi makkinan Avatar answered Oct 08 '22 04:10

woolfi makkinan