How to send line break with curl?
I've tried the following to send a line break with curl, but \n
is not interpreted by curl.
curl -X PUT -d "my message\n" http://localhost:8000/hello
How can I send a line break with curl?
Sometimes you want to provide the data to be sent verbatim.
The --data-binary
option does that.
Your shell is passing \
followed by n
rather than a newline to curl rather than "my message\n"
. Bash has support for another string syntax that supports escape sequences like \n
and \t
. To use it, start the string with $'
and end the string with '
:
curl -X PUT -d $'my message\n' http://localhost:8000/hello
See ANSI-C Quoting in the Bash Reference Manual
There's a much easier way!
curl -X PUT -d $'my message\n' http://localhost:8000/hello
This will use ANSI-C Quoting to insert the newline character.
No piping, no data files. See also Sending Newlines with cURL.