How to send line break with curl?

HttpCurlNewline

Http Problem Overview


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?

Http Solutions


Solution 1 - Http

Sometimes you want to provide the data to be sent verbatim.

The --data-binary option does that.

Solution 2 - Http

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

Solution 3 - Http

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.

Solution 4 - Http

The solution for someone who doesn't want to use files, and doesn't want to resort to shell escaping magic is:

curl -X POST --data-binary @- http://url.com <<EOF
line one
line two
EOF

But this is literal newlines in the post data payload, and not in form fields.

Solution 5 - Http

Had similar issue. While uploading a CSV file from Mac to cloud storage, new lines were being removed. After downloading it, the entire file looked like a single line. I tried adding different EOL characters \n \r \r\n with no success. Using --data-binary instead of -d solved the issue.

Btw this issue occurred only from Mac. -d worked just fine while making the call from CentOS machine. This very much looks like due to Mac's newline character. But don't feel like debugging any more.

Thanks a lot for your help.

curl -X PUT -d @filename.csv https://cloudstorage -H "content-type: text/csv"

vs

curl -X PUT --data-binary @filename.csv https://cloudstorage -H "content-type: text/csv"

Solution 6 - Http

(I ended up here with a slightly different question, so I'm just going to post my answer because it might help future explorers)

My solution applies to people who are sending form-style data, i.e. key/value pairs in a query string. Use the encoded line break, which is %0A, just like how an encoded space is %20. You can use http://meyerweb.com/eric/tools/dencoder/ to convert other symbols.

So if you want to set the key message to the value:

line one
another

you would send

curl --data "message=line%20one%0Aanother" http://localhost:8000/hello

Solution 7 - Http

A very easy way, just Shift-Enter in the console for the break. Very readable typing it in too.

curl -d "line1
line2" http-echo.com

Server gets this: line1\nline2

Do this to remove the line break:

curl -d "line1 \
line2" http-echo.com

Server gets this: line1 line2

Solution 8 - Http

Not an answer to your question, but I would work around it by creating a temporary file containing the message and line break, and give curl that file to work on:

curl -X PUT -d @message.txt http://localhost:8000/hello

From the manual:

> If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be URL-encoded. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data @foobar.

Solution 9 - Http

I was using Sendgrid with this code (copied below) originally found here https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html

\n\n worked in Gmail, but \n was ignored. I tried to double the escape and other suggestions. I also tried \r\n and that did not work in Gmail either. Note: I didn't bother to test other email clients, maybe it was a Gmail-specific problem.

    curl --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"personalizations": [{"to": [{"email": "[email protected]"}]}],"from": {"email": "[email protected]"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'

Eventually I gave up looking for a solution and switched the text/plain to text/html and just used <br /> tags.

Someone suggested that Sendgrid converts plaintext to HTML if you have a tracking pixel enabled, which makes sense. Maybe the newlines were destroyed in the plaintext-to-html conversion process. I assume the client wants a tracking pixel, so decided to switch to HTML.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeamonView Question on Stackoverflow
Solution 1 - HttpSzocskeView Answer on Stackoverflow
Solution 2 - HttpBenjamin AtkinView Answer on Stackoverflow
Solution 3 - HttpDave KerrView Answer on Stackoverflow
Solution 4 - HttpJammerView Answer on Stackoverflow
Solution 5 - HttpSutuwaShellView Answer on Stackoverflow
Solution 6 - HttpMalcolmOceanView Answer on Stackoverflow
Solution 7 - HttpJohn WilliamsView Answer on Stackoverflow
Solution 8 - HttpPekkaView Answer on Stackoverflow
Solution 9 - HttpPJ BrunetView Answer on Stackoverflow