<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>CURL on dwmkerr.com</title><link>https://dwmkerr.com/categories/curl/</link><description>Recent content in CURL on dwmkerr.com</description><generator>Hugo -- gohugo.io</generator><language>en-uk</language><managingEditor>Dave Kerr</managingEditor><copyright>Copright &amp;copy; Dave Kerr</copyright><lastBuildDate>Tue, 03 May 2016 22:12:28 +0000</lastBuildDate><atom:link href="https://dwmkerr.com/categories/curl/index.xml" rel="self" type="application/rss+xml"/><item><title>Quick Tip: Sending Newlines with cURL</title><link>https://dwmkerr.com/quick-tip-sending-newlines-with-curl/</link><pubDate>Tue, 03 May 2016 22:12:28 +0000</pubDate><guid>https://dwmkerr.com/quick-tip-sending-newlines-with-curl/</guid><description>&lt;p&gt;Yikes, this took far too long to figure out!&lt;/p&gt;
&lt;p&gt;I have a service which takes plain text multi-line input and outputs an object for each line, something like this:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Line 1
Line 2
Line 3
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;[
{line: &amp;#34;Line 1&amp;#34;},
{line: &amp;#34;Line 2&amp;#34;},
{line: &amp;#34;Line 3&amp;#34;}
]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There&amp;rsquo;s a bit more to it than that, but that&amp;rsquo;s the gist.&lt;/p&gt;
&lt;p&gt;I want to test my service with cURL, trying:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;curl --data &amp;#34;Line 1\nLine 2\nLine 3&amp;#34; \
-H &amp;#34;Content-Type: text/plain&amp;#34; localhost:3000/parse
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This did not work. Nor did some alternatives. And I really didn&amp;rsquo;t want to have to write the text to a file and load it in.&lt;/p&gt;
&lt;p&gt;Turns out there&amp;rsquo;s a nice little shell trick to let you use escape characters C style, use &lt;code&gt;$'some\ncontent'&lt;/code&gt; to use ANSI C escaping. Now you can cURL with newlines!&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;curl --data $&amp;#39;Line 1\nLine 2\nLine 3&amp;#39; \
-H &amp;#34;Content-Type: text/plain&amp;#34; localhost:3000/parse
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;h2 id="references"&gt;References&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html"&gt;GNU Bash ANSI C Quoting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/8467424/echo-newline-in-bash-prints-literal-n"&gt;Stack Overflow - Echo Newline Bash Prints \n&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/3872427/how-to-send-line-break-with-curl"&gt;Stack Overflow - How to send line break with cURL&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</description><category>CodeProject</category></item></channel></rss>