Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl gives error when piped to head command

This works great, no errors:

$ 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
...

This gives the following error:

$ 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)

It doesn't fail when I pipe to grep and tail, but fails when I pipe to head (even without arguments).

I get what I want, but it gives an error. The very last number (186 in the above example) changes every time. I just ran it three more times and got 1650, 3988, and 923.

I've tried running it with the -B option. If it's helpful, I'm on OSX 10.9. I have no ~/.curlrc. Here's the output of 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

What's going wrong here?

like image 964
Evan Hahn Avatar asked Apr 28 '14 01:04

Evan Hahn


1 Answers

head is closing the pipe before curl is done writing to it. You can disable the buffering in curl with the -N flag, causing all of the output to be written to the pipe in one big chunk, so that head will be able to operate on the whole response:

curl -sNL https://coinbase.com/api/v1/prices/historical | head -n 1
like image 66
Drew Avatar answered Sep 29 '22 13:09

Drew