我正在使用xmlhttprequest发布,并获取响应内容gzipped(故意)。 我尝试使用zlib解压缩它,但它似乎仅适用于响应对象,其他nodejs模块也无益。还有其他简单的方法吗?
这是我的代码:
function doPost(url, body, onSuccess) {
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var httpReq = new XMLHttpRequest();
httpReq.open('POST', url, true);
httpReq.setRequestHeader('Content-Type', 'application/json');
httpReq.setDisableHeaderCheck(true)
httpReq.setRequestHeader('Accept-Encoding', 'gzip');
httpReq.onreadystatechange = function () {
if (httpReq.readyState == 4 && httpReq.status == 200) {
if (httpReq.responseText.Error == undefined) {
if (typeof onSuccess === 'function') {
// unzip here...
onSuccess(JSON.parse(httpReq.responseText));
}
} else {
throw 'error in dopost: ' + httpReq.responseText.Error;
}
}
}
httpReq.send(JSON.stringify(body));
}