Nodejs request library superagent response Chinese garbled solution

Superagent library installation and use

yarn add superagent
const superagent = require('superagent')
const headers = {
    
    
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'Content-Type': 'text/html;charset=utf-8'
        }
   
try {
    
    
	superagent('GET', url).set(headers).end((req, res) => {
    
    
		console.log(res)
		}).catch(e=>{
    
    
			console.log(e)
			})
        }
        catch (e) {
    
    

        }

Chinese garbled characters appear in the response
Insert picture description here

Install superagent charset support library superagent-charset
yarn add superagent-charset

Add dependencies in the imported library

const charset = require('superagent-charset');
const superagent = charset(require('superagent'));

superagent('GET', url).charset('gbk').set(headers).end((req, res)

Warehouse link
npm-superagent-charset

Guess you like

Origin blog.csdn.net/Ruffaim/article/details/109690714