如何通过分块连续管道卷曲和张贴?

时间:2016-12-09 21:49:00

标签: http curl post chunked-encoding

我知道curl可以将数据发布到URL:

$ curl -X POST http://httpbin.org/post -d "hello"
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "hello": ""
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "5", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.50.1"
  }, 
  "json": null, 
  "origin": "64.238.132.14", 
  "url": "http://httpbin.org/post"
}

我知道我可以卷起来实现同样的目标:

$ echo "hello" | curl -X POST http://httpbin.org/post -d @-
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "hello": ""
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "5", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.50.1"
  }, 
  "json": null, 
  "origin": "64.238.132.14", 
  "url": "http://httpbin.org/post"
}

现在这里变得棘手。我知道http传输编码和分块,例如发送多行文件:

('傻'是一个包含几行文字的文件,如下所示)

$ cat silly | curl -X POST --header "Transfer-Encoding: chunked" "http://httpbin.org/post" --data-binary @-
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "hello\nthere\nthis is a multiple line\nfile\n": ""
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "41", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.50.1"
  }, 
  "json": null, 
  "origin": "64.238.132.14", 
  "url": "http://httpbin.org/post"
}

现在我想要做的是让curl从stdin读取一行,将其作为一个块发送,然后再回来再次读取stdin(这使我可以继续保持它)。这是我的第一次尝试:

curl -X POST --header "Transfer-Encoding: chunked" "http://httpbin.org/post" -d @-

只有当我按下ctrl-D时它才能正常工作,但这显然会结束卷曲的执行。

有没有办法告诉curl“发送(使用块编码)到目前为止我给你的东西,然后再回到stdin”?

非常感谢,我一直在这个问题上摸不着头脑!

0 个答案:

没有答案