我对分块编码有问题。这是一个示例代码:
opts={
hostname: "httpbin.org",
path: '/post',
method: "POST",
headers: {
"Content-Type": "text/plain", // should default to chunked
'Connection': 'keep-alive',
},
};
var req = http.request(opts, function(res){
res.setEncoding('utf8');
res.on("data", function(data){
console.log(data);
});
});
req.write("hello");
req.end();
Data returned from httpbin:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Connection": "close",
"Content-Type": "text/plain",
"Host": "httpbin.org",
"Transfer-Encoding": "chunked",
}
注意数据为空。
但是,一旦添加'Content-Length': 5,
到opts.headers
,分块编码将被禁用,数据将按预期变为“ hello”。
分块编码有什么问题?
这似乎是httpbin的已知问题。您没有在客户端做任何错误的事情。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句