我正在针对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-9
,a-z
,A-Z
,+
和/
的字符串。
this.query['Signature']
是网址的一部分。
答案 0 :(得分:7)
我终于解决了它,使用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 ß