0
我正在使用xmlhttprequest發佈,並獲取響應內容gzipped(故意)。 我試圖使用zlib解壓縮它,但它似乎只適用於響應對象,其他nodejs模塊也無濟於事。還有其他簡單的方法可以做到嗎?node.js gzip解壓縮xmlhttprequesr.responseText
這裏是我的代碼:
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));
}