Umlauts broken when doing get request

社会主义新天地 提交于 2019-12-11 19:48:38

问题


I'm trying to query a webservice which answers with plain text. The text often has german umlauts in it. In the received stream the umlauts are broken. Any ideas what am I doing wrong?

Regards, Torsten

Here is the sample code:

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

And the response:

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

回答1:


When you set the encoding option in your request call, you advise the request module to decode the response body with this encoding. In this way you ignore the encoding used by the webservice, wich may or may not be utf-8. You need to find out wich encoding was used be the webservice and use that.

Depending on how complient the webservice you could also try to set the Accept-Charset: utf-8 header.

As your output shows, the webservice doesn't provide the used encoding in the Content-Type header, which is a bad habbit imho.

Sidenote: Content-Encoding isn't for charset, but for compression, gzip migh be a valid value for it.



来源:https://stackoverflow.com/questions/23119411/umlauts-broken-when-doing-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!