Node.js请求将变音符号转换为

chr*_*con 9 javascript encoding node.js

我正在针对Web服务执行以下请求(使用请求/请求):

return request.postAsync({
    url,
    charset: 'Cp1252', // I also tried utf-8
    encoding: null, //
    // I also tried Cp1252 -> unknown encoding,
    // I also tried utf-8 and nothing at all
    headers: {
         "Accept": "application/octet-stream, text, text/plain, text/xml",
         "Accept-Encoding": "UTF-8",
         'Content-Type': "text/plain; charset=Cp1252;", // also tried utf-8, ISO-8859-1
         "User-Agent": "me"
    }
}).spread((res, body) => {
    body = body.toString();  // I also tried without toString();
    let ws = fs.createWriteStream('hhh.csv');
    ws.write(body);
    ws.end();
Run Code Online (Sandbox Code Playgroud)

无论我做什么,变形金刚都变成了?.

这些是Web服务发回的标头:

'content-type': 'text; charset=Cp1252',
'content-length': '1895980',
vary: 'Accept-Encoding,User-Agent'
Run Code Online (Sandbox Code Playgroud)

我正在尝试这几天没有运气.我究竟做错了什么?

这是目前为止没有解决我问题的问题/答案列表:

可能是以下之一导致我的输入字符串不是UTF-8?

let hash = crypto.createHmac("sha256", this.options.Signer);
this.query['Signature'] = hash.update(stringToSign).digest("base64");
Run Code Online (Sandbox Code Playgroud)

签名人是包含字符串0-9,a-z,A-Z,+,和/.

this.query['Signature'] 是URL的一部分.

chr*_*con 6

我终于解决了它,使用iconv-lite并设置请求的编码¹null,使其作为Buffer返回正文.这是我现在正在工作的配置:

return request.getAsync({
        url,
        encoding: null,
        headers: {
            "Accept": "text, text/plain, text/xml",
            "Accept-Encoding": "UTF-8",
            'Content-Type': "text/plain; charset=utf-8;",
            "User-Agent": "me"
        }
    }).spread((res, body) => {
        let a = iconv.decode(new Buffer(body), 'cp1252');
        // now a is holding a string with correct Umlauts and ß
Run Code Online (Sandbox Code Playgroud)