nodejs 请求库 superagent response 中文乱码解决办法

superagent 库安装与使用

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) {
    
    

        }

响应出现中文乱码
在这里插入图片描述

安装 superagent charset 支持库 superagent-charset
yarn add superagent-charset

在引入库加入依赖

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

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

仓库链接
npm - superagent-charset

猜你喜欢

转载自blog.csdn.net/Ruffaim/article/details/109690714