当管道输入头部命令时,curl会出错

时间:2014-04-28 01:28:11

标签: curl stream pipe head

这很好用,没有错误:

$ curl -sSL https://coinbase.com/api/v1/prices/historical
2014-04-27T18:19:17-07:00,430.52
2014-04-27T18:10:24-07:00,436.25
2014-04-27T17:56:57-07:00,436.14
...

这会出现以下错误:

$ curl -sSL https://coinbase.com/api/v1/prices/historical | head -n 1
2014-04-27T18:19:17-07:00,430.52
curl: (23) Failed writing body (0 != 186)

当我输送到greptail时它不会失败,但是当我输送到head(即使没有参数)时它会失败。

我得到了我想要的东西,但它给出了一个错误。最后一个数字(上例中的186)每次都会改变。我刚跑了三次,得到1650,3988和923。

我尝试使用-B选项运行它。如果它有用,我在OSX 10.9上。我没有~/.curlrc。这是curl --version

的输出
curl 7.30.0 (x86_64-apple-darwin13.0) libcurl/7.30.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IPv6 Largefile NTLM NTLM_WB SSL libz

这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

head正在关闭管道,curl写完之前。您可以使用-N标志禁用curl中的缓冲,从而将所有输出写入一个大块中的管道,以便head能够对整个响应进行操作:

curl -sNL https://coinbase.com/api/v1/prices/historical | head -n 1