分块编码不起作用

时间:2014-07-10 16:11:45

标签: node.js

我遇到了分块编码的问题。这是一个示例代码:

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",
    "X-Request-Id": "341e3bea-9c71-4b69-bff3-93cea7207c6d"
  },
  "json": null,
  "origin": "119.xxx,xxx",
  "url": "http://httpbin.org/post"
}

注意到数据是空的。

但是,一旦我将'Content-Length': 5,添加到opts.headers,就会禁用分块编码,数据变为" hello"如预期的那样。

分块编码有什么问题?

1 个答案:

答案 0 :(得分:0)

这似乎是带有httpbin的known issue。你没有做任何错误的客户端。