Quantcast
Viewing all articles
Browse latest Browse all 59

Quick Tip: Sending Newlines with cURL

Image may be NSFW.
Clik here to view.
Quick Tip: Sending Newlines with cURL

Yikes, this took far too long to figure out!

I have a service which takes plain text multiline input and outputs an object for each line, something like this:

Input

Line 1  
Line 2  
Line 3  

Output

[
  {line: "Line 1"},
  {line: "Line 2"},
  {line: "Line 3"}
]

There's a bit more to it than that, but that's the gist.

I want to test my service with cURL, trying:

curl --data "Line 1\nLine 2\nLine 3" \  
  -H "Content-Type: text/plain" localhost:3000/parse

Did not work, not did some alternatives, and I really didn't want to have to write the text to a file and load it in.

Turns out there's a nice little shell trick to let you use escape characters C style, use $'some\ncontent' to use ANSI C escaping. Now you can cURL with newlines!

curl --data $'Line 1\nLine 2\nLine 3' \  
  -H "Content-Type: text/plain" localhost:3000/parse

Enjoy!

References

  1. GNU Bash ANSI C Quoting
  2. Stack Overflow - Echo Newline Bash Prints \n
  3. Stack Overflow - How to send line break with cURL

Viewing all articles
Browse latest Browse all 59

Trending Articles