【问题标题】:curl gives error when piped to head commandcurl 传递到 head 命令时出错
【发布时间】:2014-06-13 09:58:33
【问题描述】:

这很好用,没有错误:

$ 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)每次都会改变。我又运行了 3 次,得到 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

这里出了什么问题?

【问题讨论】:

    标签: curl stream pipe head


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您回答我两年前的问题!
    猜你喜欢
    • 2017-08-29
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2021-09-08
    相关资源
    最近更新 更多