Home Web Front-end Front-end Q&A What to do if node http get is garbled?

What to do if node http get is garbled?

Dec 29, 2022 pm 04:10 PM
node Garbled characters

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:

1

2

3

4

5

6

7

8

9

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve garbled word page numbers How to solve garbled word page numbers Jun 25, 2023 pm 03:23 PM

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.

How to solve Chinese garbled characters in Linux How to solve Chinese garbled characters in Linux Feb 21, 2024 am 10:48 AM

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

How to solve tomcat startup garbled code How to solve tomcat startup garbled code Dec 26, 2023 pm 05:21 PM

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.

How to solve the problem of Chinese garbled characters in Windows 10 How to solve the problem of Chinese garbled characters in Windows 10 Jan 16, 2024 pm 02:21 PM

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 characters in win11 system documents How to solve the problem of garbled characters in win11 system documents Jun 29, 2023 pm 06:29 PM

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.

How to solve filezilla garbled characters How to solve filezilla garbled characters Nov 20, 2023 am 10:16 AM

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.

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

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!

Editing method to solve the problem of garbled characters when opening dll files Editing method to solve the problem of garbled characters when opening dll files Jan 06, 2024 pm 07:53 PM

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

See all articles