【问题标题】:Node.js request turns umlauts to �Node.js 请求将变音符号变为 �
【发布时间】:2016-01-02 16:37:10
【问题描述】:

我正在对 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();

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

这些是 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 的一部分。

【问题讨论】:

  • 您发送的确切字符是什么?它们目前是如何编码的,它们的当前字符集是什么?
  • 在元音变音为“还是”元音变音的地方。
  • "node js 请求将元音变音变为..." "无论我做什么,元音变音都转为...。" --- 这些短语是什么意思?
  • 您发送的确切字符是什么?它们目前是如何编码的,它们的当前字符集是什么?
  • 您是否尝试过更改writeStreamdefaultEncoding?即fs.createWriteStream('hhh.csv', {defaultEncoding: 'utf8'});

标签: javascript node.js encoding


【解决方案1】:

我终于解决了,使用iconv-lite 并将请求的编码¹ 设置为null,使其将主体作为缓冲区返回。这是我现在的工作配置:

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 ß

【讨论】:

猜你喜欢
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 2013-07-05
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多