【问题标题】:Umlauts broken when doing get request执行获取请求时变音符号损坏
【发布时间】:2014-05-31 21:56:44
【问题描述】:

我正在尝试查询以纯文本形式回答的网络服务。文本中通常有德语umlauts。在接收到的流中,变音符号被破坏了。任何想法我做错了什么?

问候, 托斯滕

这里是示例代码:

var request = require('request');        
var uri = <anUriWithUserId>;        
request(uri, {encoding: 'utf8','content-type': 'text/plain; charset=UTF-8'}, 
    function (error, response, body) 
    {
        console.log("encoding: " + response.headers['content-encoding']);
        console.log("type: " + response.headers['content-type']);
        console.log(body);
    }); 

然后回应:

encoding: undefined
type: text/plain

error=0
---
asin=
name=Eistee
detailname=Pfanner Der Gr�ne Tee, Zitrone - Kaktusfeige, 2,0 l
vendor=Hermann Pfanner Getr�nke GmbH, Lauterach, �sterreich
maincat=Getr�nke, Alkohol

【问题讨论】:

标签: node.js http httprequest http-get diacritics


【解决方案1】:

当您在请求调用中设置encoding 选项时,您建议请求模块使用此编码解码响应正文。通过这种方式,您可以忽略 web 服务使用的编码,它可能是也可能不是 utf-8。您需要找出用于 web 服务的编码并使用它。

根据 web 服务的兼容性,您还可以尝试设置 Accept-Charset: utf-8 标头。

正如您的输出所示,Web 服务未在 Content-Type 标头中提供使用的编码,恕我直言,这是一个坏习惯。

旁注:Content-Encoding 不是用于 charset,而是用于压缩,gzip 可能是它的有效值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2019-06-13
    相关资源
    最近更新 更多