255

It works ok as a single tool:

curl "someURL"
curl -o - "someURL"

but it doesn't work in a pipeline:

curl "someURL" | tr -d '\n'
curl -o - "someURL" | tr -d '\n'

it returns:

(23) Failed writing body

What is the problem with piping the cURL output? How to buffer the whole cURL output and then handle it?

Matthias Braun
34.9k27 gold badges158 silver badges177 bronze badges
asked May 23, 2013 at 0:16
11
  • 1
    For me it works, no need to buffer. Commented May 23, 2013 at 0:20
  • 1
    does this work in pipeline too?: curl 'http://www.multitran.ru/c/m.exe?CL=1&s=hello&l1=1' | tr -d '\n' Commented May 23, 2013 at 0:22
  • 1
    Added osx tags. Unfortunately I cannot help with this. I'm using Linux Commented May 23, 2013 at 0:29
  • 1
    the problem was encoding of the page (cyrilic, win-1251). So I must use iconv -f ... Commented May 23, 2013 at 0:59
  • 5
    Just as another hint: Mine failed, because the disk was full. Commented Oct 26, 2016 at 12:32

20 Answers 20

182

This happens when a piped program (e.g. grep) closes the read pipe before the previous program is finished writing the whole page.

In curl "url" | grep -qs foo, as soon as grep has what it wants it will close the read stream from curl. cURL doesn't expect this and emits the "Failed writing body" error.

A workaround is to pipe the stream through an intermediary program that always reads the whole page before feeding it to the next program.

E.g.

curl "url" | tac | tac | grep -qs foo

tac is a simple Unix program that reads the entire input page and reverses the line order (hence we run it twice). Because it has to read the whole input to find the last line, it will not output anything to grep until cURL is finished. Grep will still close the read stream when it has what it's looking for, but it will only affect tac, which doesn't emit an error.

answered Mar 5, 2015 at 13:58
Sign up to request clarification or add additional context in comments.

8 Comments

Could you not simply pipe it through cat once? Solves the issue for me, at least.
No. It might help with small documents but when it is too large to fit in the buffer cat uses the error will reappear.You could use -s to silence all error messages (and progress) if you don't need them.
tac|tac changes the input if input does not end with a linefeed, or for example printf a\\nb\\nc|tac|tac prints a\ncb where \n is a linefeed. You can use sponge /dev/stdout instead. Another option is printf %s\\n "$(cat)", but when the input contains null bytes in shells other than Zsh, that either skips the null bytes or stops reading after the first null byte.
This should be accepted answer because it explains the problem, altought it does not provide capable solution as there is no tac command on macOS
I simply just ignore stderr and sent it to null: curl "url" 2>/dev/null | grep
|
82

For completeness and future searches:

It's a matter of how cURL manages the buffer, the buffer disables the output stream with the -N option.

Example: curl -s -N "URL" | grep -q Welcome

Matthias Braun
34.9k27 gold badges158 silver badges177 bronze badges
answered Feb 23, 2016 at 13:36

2 Comments

It did work for curl -s https://raw.githubusercontent.com/hermitdave/FrequencyWords/master/content/2016/ro/ro_50k.txt | head -20 (without -s I get the same error).
-s just makes curl silent so it doesn't emit the error you'd otherwise see. The underlying issue is still happening, but for most situations that's fine. However if you were doing something like curl ... | tee /tmp/full_output | head -20 then you need to actually resolve the error if you want /tmp/full_output to have everything.
69

Another possibility, if using the -o (output file) option - the destination directory does not exist.

eg. if you have -o /tmp/download/abc.txt and /tmp/download does not exist.

Hence, ensure any required directories are created/exist beforehand, use the --create-dirs option as well as -o if necessary

answered Jan 24, 2018 at 10:46

2 Comments

Thanks, --create-dirs solved this for me in the most unusual situation, couldn't ever figure out what was wrong, but this was the ticket!
It just happened to me in similar case. I forgot to declare the variable $out for the output. Thanks, Mike.
16

You can do this instead of using -o option:

curl [url] > [file]

answered Jul 14, 2013 at 19:07

1 Comment

so, not using the pipe and instead do all the work over the file system? I wanted to use the curl's output with pipes.
16

The server ran out of disk space, in my case.

Check for it with df -k .

I was alerted to the lack of disk space when I tried piping through tac twice, as described in one of the other answers: https://stackoverflow.com/a/28879552/336694. It showed me the error message write error: No space left on device.

answered Feb 20, 2018 at 15:59

1 Comment

I received the same error due to running out of disk space within a container, for anyone else also hitting the same issue can clean up space within their containers with docker system prune
12

If you are trying something similar like source <( curl -sS $url ) and getting the (23) Failed writing body error, it is because sourcing a process substitution doesn't work in bash 3.2 (the default for macOS).

Instead, you can use this workaround.

source /dev/stdin <<<"$( curl -sS $url )"
answered Aug 31, 2019 at 0:05

Comments

11

So it was a problem of encoding. Iconv solves the problem

curl 'http://www.multitran.ru/c/m.exe?CL=1&s=hello&l1=1' | iconv -f windows-1251 | tr -dc '[:print:]' | ...
answered May 23, 2013 at 11:00

Comments

10

Trying the command with sudo worked for me. For example:

sudo curl -O -k 'https url here'

note: -O (this is capital o, not zero) & -k for https url.

answered Sep 26, 2021 at 2:32

5 Comments

explaination please?
it's already mentioned in the answer as note.
The crux of the answer lies in the usage of sudo. Can you please explain why it works with escalated privileges?
@markroxor one reason using sudo would resolve the issue is if you are trying to download a file using curl and the output path is restricted. Using sudo in the command would allow for the creation of the file. Another work around would be to change the permissions on the folder you are outputting the file to so sudo permissions would not be required.
If that is the reason, I believe that it does more bad than good. If user is aware that he is writing files in the directory where he doesn't have enough permissions he should switch user before proceeding. Downloading files from the internet as a root user is never a good idea. @DanielBlack
7

I encountered the same problem when doing:

curl -L https://packagecloud.io/golang-migrate/migrate/gpgkey | apt-key add -

The above query needs to be executed using root privileges.

Writing it in following way solved the issue for me:

curl -L https://packagecloud.io/golang-migrate/migrate/gpgkey | sudo apt-key add -

If you write sudo before curl, you will get the Failed writing body error.

Gerhardh
12.4k4 gold badges19 silver badges47 bronze badges
answered Jan 7, 2021 at 5:47

Comments

6

I had the same error but from different reason. In my case I had (tmpfs) partition with only 1GB space and I was downloading big file which finally filled all memory on that partition and I got the same error as you.

answered Mar 30, 2017 at 16:04

Comments

6

In my case, I was doing: curl <blabla> | jq | grep <blibli>

With jq . it worked: curl <blabla> | jq . | grep <blibli>

answered Nov 23, 2020 at 5:13

Comments

4

For me, it was permission issue. Docker run is called with a user profile but root is the user inside the container. The solution was to make curl write to /tmp since that has write permission for all users , not just root.

I used the -o option.

-o /tmp/file_to_download

answered Mar 20, 2019 at 10:57

1 Comment

Yep, this might come as a surprise when you usually run scripts that create tmp files as non-privileged users, and then run that script as root just once to test sth. Later again, the non-privileged users won't be able to use/cleanup the temp files left by root. I try to always put a "chown user:" into my scripts just after creating the tmp file.
4

I got this error trying to use jq when I didn't have jq installed. So... make sure jq is installed if you're trying to use it.

answered Oct 26, 2022 at 0:22

1 Comment

I was getting the same error and I was sure JQ is installed already. But turned out it wasn't -- but curl was hiding the missing binary error.
3

The explanation here by @Kaworu is great: https://stackoverflow.com/a/28879552/198219

This happens when a piped program (e.g. grep) closes the read pipe before the previous program is finished writing the whole page. cURL doesn't expect this and emits the "Failed writing body" error.

A workaround is to pipe the stream through an intermediary program that always reads the whole page before feeding it to the next program.

I believe the more correct implementation would be to use sponge, as already suggested by @nisetama in the comments:

curl "url" | sponge | grep -qs foo

answered Sep 28, 2022 at 16:30

1 Comment

In order to install sponge: sudo apt-get install moreutils
2

I encountered this error message while trying to install varnish cache on ubuntu. The google search landed me here for the error (23) Failed writing body, hence posting a solution that worked for me.

The bug is encountered while running the command as root curl -L https://packagecloud.io/varnishcache/varnish5/gpgkey | apt-key add -

the solution is to run apt-key add as non root

curl -L https://packagecloud.io/varnishcache/varnish5/gpgkey | apt-key add -
answered Feb 1, 2018 at 9:35

Comments

1

I was getting curl: (23) Failed writing body . Later I noticed that I did not had sufficient space for downloading an rpm package via curl and thats the reason I was getting issue. I freed up some space and issue for resolved.

answered Sep 10, 2022 at 17:58

Comments

1

I had the same question because of my own typo mistake:

# fails because of reasons mentioned above
curl -I -fail https://www.google.com | echo $? 
curl: (23) Failed writing body
# success
curl -I -fail https://www.google.com || echo $?
answered Jan 17, 2023 at 15:39

Comments

0

In Bash and zsh (and perhaps other shells), you can use process substitution (Bash/zsh) to create a file on the fly, and then use that as input to the next process in the pipeline chain.

For example, I was trying to parse JSON output from cURL using jq and less, but was getting the Failed writing body error.

# Note: this does NOT work
curl https://gitlab.com/api/v4/projects/ | jq | less

When I rewrote it using process substitution, it worked!

# this works!
jq "" <(curl https://gitlab.com/api/v4/projects/) | less

Note: jq uses its 2nd argument to specify an input file

Bonus: If you're using jq like me and want to keep the colorized output in less, use the following command line instead:

jq -C "" <(curl https://gitlab.com/api/v4/projects/) | less -r

(Thanks to Kowaru for their explanation of why Failed writing body was occurring. However, their solution of using tac twice didn't work for me. I also wanted to find a solution that would scale better for large files and tries to avoid the other issues noted as comments to that answer.)

answered May 14, 2020 at 18:15

Comments

0

Don't use tac | tac, use the stdbuf:

curl .... | stdbuf -o1M jq '.'

stdbuf was designed for this purpose.

answered May 28, 2025 at 21:00

Comments

-2

I added flag -s and it did the job. eg: curl -o- -s https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

answered Sep 27, 2022 at 7:53

1 Comment

This only hides the error ;-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.