4

When I curled a too long API call and wanted to show only the first 30 lines in the console with curl my_api_call|head -30, I ran into:

 24 49931   24 12089    0     0    98k      0 --:--:-- --:--:-- --:--:--   99k
curl: (23) Failure writing output to destination

This is surely the same thing going on as in curl: (23) Failure writing output to destination, but there, it fails with the download. Here, it fails right after piping to "head"/"tail". The piping itself still works, I get the 30 lines. If I take the pipe out, there is no failure.

The answer of the other question could not help me, I ran sudo apt remove curl and sudo apt install curl.

What can I do to get rid of the failure? Up to now, I do nothing, that is already working well. Is it a bug?

3 Answers3

2

Found workaround

I was able to suppress the error message curl: (23) Failure writing output to destination with following command line:

RESPONSE=$(curl http://httpstat.us/502); head -n 1 <<< $RESPONSE

The steps in detail

  1. Tell curl to write the full content into a variable (which prevents the error message from curl). If you like, you could also check for error codes after this step.
  2. Forward the variable to head
Jochen
  • 146
1

As per my analysis, the error might be reasonable if the head command cuts off some data or it can display the full result.

I checked with curl http://www.google.com | head -n 10 && echo "Success" || echo Failure (=partial contant) and with curl http://www.google.com | head -n 1000 && echo "Success" || echo Failure (=full content) and I always get curl: (23) Failure writing output to destination in case of the 10-lines variant. The 1000-lines variant always executes without this error message.

Interestingly, my test shows "Success" in both cases. Also (curl http://www.google.com | head -n 10); echo $? always shows an error code 0 in the end. BUT: my GitLab pipeline somehow detects that this command failed and shows my gitlab-job as failed.

Jochen
  • 146
1

Well, curl apparently checks if it fails to write to stdout.

$ man curl type /^ +-S and hit enter, then read a couple lines beyond that and you'll find an option that is likely to be useful for you. (i.e. --silent )

If you still wish to see the 30 first lines; redirect output to a a file in /tmp/, then do a head -n 30 on that file.

Hannu
  • 6,605
  • 1
  • 28
  • 45