What to do if node http get is garbled?

藏色散人
Release: 2022-12-29 16:10:15
Original
2463 people have browsed it

Node http get garbled solution: 1. Open the corresponding react file; 2. Pass "var req = http.get(url,function(res){res.setEncoding('utf-8') ;var html = ''res.on('data',function(data){html =data.toString();})..." statement can be programmed to "utf-8".

What to do if node http get is garbled?

The operating environment of this tutorial: Windows 10 system, node v10.16.0 version, Dell G3 computer.

What should I do if node http get is garbled?

nodejs http.get garbled problem handling method

The code is as follows:

var req = http.get(url,function(res){
    res.setEncoding('utf-8');
    var html = ''
    res.on('data',function(data){
        html+=data.toString();
    }).on('end',function(){
        console.log(html);
    })
});
Copy after login

Related introduction:

http. get:

Since most requests are GET requests without a body, Node.js provides this convenience method. The only difference between this method and http.request() is that it sets the method to GET and Automatically call req.end(). Note that due to the reasons described in the http.ClientRequest chapter, the callback must pay attention to consuming response data.

Mainly used for making data requests.

About http Interpretation of .get code:

const http =require('http');//由于http.get是Node的http模块   所以第一件事情当然是引入http模块啦~
                    
            http.get('这里是你想要请求的接口地址', (res) => {//res是请求后端给你的数据
               
                const { statusCode } = res;//获取请求的状态码
                
                const contentType = res.headers['content-type'];//获取请求类型
              
                let error;
                if (statusCode !== 200) {//如果请求不成功 (状态码200代表请求成功哦那个)
                  error = new Error('请求失败\n' +
                                    `状态码: ${statusCode}`); //报错抛出状态码
                } else if (!/^application\/json/.test(contentType)) {//验证请求数据类型是否为json数据类型   json的content-type :'content-type':'application/json'
                  error = new Error('无效的 content-type.\n' +//再次报错
                                    `期望的是 application/json 但接收到的是 ${contentType}`);
                }
                if (error) {//如果报错了
                  console.error(error.message);
        res.resume();//将请求的错误存入日志文件
                  return;
                }
              
              //请求成功
                res.setEncoding('utf8');//字符编码设为万国码
                let rawData = '';//定义一个字符变量
                res.on('data', (chunk) => { rawData += chunk; });//通过data事件拼接数据流得到数据
                res.on('end', () => {//end表示获取数据结束了
                  try {  //捕获错误信息
                   
                    console.log(rawData);//输出数据
                  } catch (e) {
                    console.error(e.message);
                  }
                });
              }).on('error', (e) => {
                console.error(`出现错误: ${e.message}`);
              });
Copy after login

Recommended study: "node.js Video Tutorial"

The above is the detailed content of What to do if node http get is garbled?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!