2015-10-05 60 views
7

我做的对Web服务的下列请求(使用请求/请求):Node.js的要求变成变音符号来

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(); 

无论我做什么,变音符号都变成

这些都是头Web服务发送回:

'content-type': 'text; charset=Cp1252', 
'content-length': '1895980', 
vary: 'Accept-Encoding,User-Agent' 

我没有运气可言尝试这几天。我究竟做错了什么?

这里的问题/答案的列表,并没有解决我的问题至今:

它可以是下列之一引起我的输入字符串不为UTF-8?

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

签名者是含有0-9a-zA-Z+,和/的字符串。

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

+0

什么是您要发送的确切字符?他们目前的编码方式如何,当前的字符集是什么? – zerkms

+0

在元音变“仍”变音符的地方。 – zerkms

+0

“节点js请求将元音变成 ”“无论我做什么,变音都变成 。” ---这些短语意味着什么? – zerkms

回答

7

我终于解决了它,使用iconv-lite和设置请求的编码1到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 ß 
+0

http://www.utf8everywhere.org/ – zerkms

+1

相关:http://stackoverflow.com/questions/12040643/nodejs-encoding-using-request –