Solve the problem of garbled code when the return value of http request is html in nodejs

  • 2021-07-22 08:26:55
  • OfStack

Today, when I made an http request with nodejs, the data returned was an html file, and then I followed the previous method of parsing json data. Sure enough, an error was reported: SyntaxError: Unexpected token in JSON at position 0

No way, had to change a method, will receive the Buffer object toString, and then print it out and find that it is garbled.

The first feeling is the coding problem. Then look at the official documents under google1 and summarize three methods:

1. toString plus encoding format as parameters.

2. Use iconv-lite to change the coding.

3. Load html using cheerio.

However, the above three methods are not my mistakes, and then I saw that there are people in cnode who have similar problems. Although they are different, some of the people who answered below mentioned using gzip compression, which will lead to garbled code if they don't decompress after receiving it. Then I found gzip compression = = manually covering my face in my request header.

Knowing the problem, it is very convenient to solve it.

First, let's go under npm install zlib 1;

Then var zlib = require ('zlib') is introduced into the head;

Then look up the official documents and find that there are two decompression methods, one is synchronous and the other is asynchronous.

I will use asynchronous method here.


zlib.unzip(chunk,function(error,res){
    console.log(error);
    console.log(res+"");
});

The chunk here is the buffer object we received. It should be noted that this asynchronous callback has two parameters, the first one seems to be an error message, and the second one is the html string we need.

If you need to use synchronized students, please call zlib. unzipSync (buffer); ps: It means that I reported an error during the test. Error: unexpected end of file

Well, here, my problem is solved perfectly.


Related articles: