What to do if node http get is garbled?
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".
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); }) });
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}`); });
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to garbled word page numbers: 1. Open the word document and click the "File" option in the upper left corner; 2. Select the "More" option, and then click the "Options" button; 3. Select "Advanced" in the word options; 4. . Find "Show field codes instead of field values" in "Show document content", remove the check in front, and click OK to return to the home page.

The Linux Chinese garbled problem is a common problem when using Chinese character sets and encodings. Garbled characters may be caused by incorrect file encoding settings, system locale not being installed or set, and terminal display configuration errors, etc. This article will introduce several common workarounds and provide specific code examples. 1. Check the file encoding setting. Use the file command to view the file encoding. Use the file command in the terminal to view the encoding of the file: file-ifilename. If there is "charset" in the output

Solutions to garbled tomcat startup: 1. Modify Tomcat's conf configuration file; 2. Modify the system language; 3. Modify the command line window encoding; 4. Check the Tomcat server configuration; 5. Check the project encoding; 6. Check the log file; 7 , try other solutions. Detailed introduction: 1. Modify Tomcat's conf configuration file, open Tomcat's conf directory, find the "logging.properties" file, etc.

In the Windows 10 system, garbled characters are common. The reason behind this is often that the operating system does not provide default support for some character sets, or there is an error in the set character set options. In order to prescribe the right medicine, we will analyze the actual operating procedures in detail below. How to solve Windows 10 garbled code 1. Open settings and find "Time and Language" 2. Then find "Language" 3. Find "Manage Language Settings" 4. Click "Change System Regional Settings" here 5. Check the box as shown and click Just make sure.

How to solve the problem of garbled text documents in win11? When many users use the win11 system, text documents are garbled and cannot be read normally. Many friends do not know how to solve this problem. In fact, this method is not difficult. Below, the editor has compiled the steps to solve the problem of garbled Windows 11 system documents. I hope it can bring you some inspiration! Steps to solve garbled Windows 11 system documents: 1. First, open the control panel of win11, enter control panel in the search box below, and click Search to enter the control panel. 2. After entering the panel, find the clock and area and click to enter, then click on the area option. 3. After entering, click on the management panel, and then click on Change system regional settings.

Solutions to filezilla garbled characters include: 1. Check the encoding settings; 2. Check the file itself; 3. Check the server configuration; 4. Try other transfer tools; 5. Update the software version; 6. Check for network problems; 7. Seek technical support. To solve the problem of FileZilla garbled characters, you need to start from multiple aspects, gradually investigate the cause of the problem, and take corresponding measures to repair it.

When many users use computers, they will find that there are many files with the suffix dll, but many users do not know how to open such files. For those who want to know, please take a look at the following details. Tutorial~How to open and edit dll files: 1. Download a software called "exescope" and download and install it. 2. Then right-click the dll file and select "Edit resources with exescope". 3. Then click "OK" in the pop-up error prompt box. 4. Then on the right panel, click the "+" sign in front of each group to view the content it contains. 5. Click on the dll file you want to view, then click "File" and select "Export". 6. Then you can

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!
