<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><language>en</language><title>Blog posts by int 80h</title> <link>https://world.optimizely.com/blogs/int-80h/</link><description></description><ttl>60</ttl><generator>Optimizely World</generator><item> <title>Oh hm. Sorry...</title>            <link>http://per.ghostify.io/oh-hm-sorry/</link>            <description>&lt;h1 id=&quot;helloitsmeagain&quot;&gt;Hello. It&#39;s me again.&lt;/h1&gt;

&lt;p&gt;Have not posted anything in a while. This is about to change. Stay tuned :) &lt;/p&gt;</description>            <guid>http://per.ghostify.io/oh-hm-sorry/</guid>            <pubDate>Sun, 15 Jun 2014 11:44:31 GMT</pubDate>           <category>Blog post</category></item><item> <title>Using curl to communicate with your webservice</title>            <link>http://per.ghostify.io/using-curl-to-communicate-with-your-webservice/</link>            <description>&lt;h1 id=&quot;tldr&quot;&gt;TL;DR&lt;/h1&gt;

&lt;p&gt;This is a post on how to use curl in as a simple way to do json http calls through your command line.  &lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;curl -X POST -H &quot;Content-Type: application/json&quot; --data-ascii &quot;@customer.json&quot; http://localhost/customers | python -mjson.tool  
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;edit&quot;&gt;Edit&lt;/h3&gt;

&lt;p&gt;Curl implicitly does most of the flags for you so the above version could be written like this:  &lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;curl -H &quot;Content-Type: application/json&quot; -d &quot;@customer.json&quot; http://localhost/customers | python -mjson.tool  
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Thx to the reddit guy who pointed this out)&lt;/p&gt;

&lt;h2 id=&quot;theproblem&quot;&gt;The Problem&lt;/h2&gt;

&lt;p&gt;Have you ever felt the need for communicating with your server side api in a slick and uncomplicated way without having to write dummy debugging apps?&lt;/p&gt;

&lt;p&gt;There are a couple of approaches for this kind of manual integration testing. And as we hackers love to use the command line or the terminal I would like to share with you the way I do it. Using &lt;a href=&quot;http://curl.haxx.se/&quot;&gt;&lt;strong&gt;Curl&lt;/strong&gt;&lt;/a&gt; and a data.json payload file which I can modify easily in my favorite text editor.  &lt;/p&gt;

&lt;h3 id=&quot;curl&quot;&gt;Curl&lt;/h3&gt;

&lt;p&gt;Curl is a tool every hacker should know about. It is basically a command line interface for doing HTTP comunication. It has gazillions of parameter flags but you don&#39;t need to master them all. There are a couple of ones you should know about though.&lt;/p&gt;

&lt;p&gt;But first thing first. You need to have curl running on your machine.&lt;/p&gt;

&lt;p&gt;If you are a GNU/Linux ninja you probably already have it installed (Otherwise use your package manager to install it). If you are a windows monkey like me you need the windows binaries. &lt;br /&gt;
Which are up for grab from here: &lt;a href=&quot;http://curl.haxx.se/download.html&quot;&gt;http://curl.haxx.se/download.html&lt;/a&gt; (Scroll down a bit to see the generic win32 or 64bit binaries)&lt;/p&gt;

&lt;p&gt;Once downloaded you can either be lazy like me and paste the binary into &lt;code&gt;c:/windows/system32&lt;/code&gt; to have access to curl.exe without any path. Or you can do it properly by creating a folder under program files which you then assign to the &lt;code&gt;PATH&lt;/code&gt; enviroment variable.&lt;/p&gt;

&lt;h3 id=&quot;tryingitout&quot;&gt;Trying it out&lt;/h3&gt;

&lt;p&gt;Now when you have curl installed, you can try it by typing &lt;code&gt;curl google.se&lt;/code&gt; in your command line. You shoud see something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot; prettyprint lang-html&quot;&gt;C:\Users\Per&amp;gt;curl google.se  
&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=utf-8&quot;&amp;gt;  
&amp;lt;TITLE&amp;gt;301 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;  
&amp;lt;H1&amp;gt;301 Moved&amp;lt;/H1&amp;gt;  
The document has moved  
&amp;lt;A href=&#39;http://www.google.se/&#39; &amp;gt;here&amp;lt;/A&amp;gt;.  
&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This bascially tells curl to send a HTTP GET request to the google.se server and the flush the response back into the command line.&lt;/p&gt;

&lt;p&gt;It can be handy if you want to dump the html of a page into a file. Just pipe the response with the &lt;code&gt;&amp;gt;&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;Like this: &lt;code&gt;curl google.se &amp;gt; google.html&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;postingsomejson&quot;&gt;Posting some JSON&lt;/h4&gt;

&lt;p&gt;Anyway, we want to do cooler stuff, like sending JSON to our web service. But we need to learn a little about a couple of curl flags first. &lt;/p&gt;

&lt;p&gt;First up is the &lt;code&gt;-X&lt;/code&gt; flag. You can use it to choose &lt;a href=&quot;http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html&quot;&gt;HTTP request method&lt;/a&gt;. Together with any HTTP verb like: POST, PUT, UPDATE, it will tell curl to switch request method.&lt;/p&gt;

&lt;p&gt;Next up is the flag for reading the request payload from disk instead of specifying it directly with the command (which also is possible).&lt;/p&gt;

&lt;p&gt;It is done with the &lt;code&gt;--data-&amp;lt;type&amp;gt;&lt;/code&gt; flag. The type can vary depending on what you want to send. &lt;/p&gt;

&lt;p&gt;Here is what the help section says:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot; prettyprint&quot;&gt; -d/--data &amp;lt;data&amp;gt;   HTTP POST data (H)
    --data-ascii &amp;lt;data&amp;gt;  HTTP POST ASCII data (H)
    --data-binary &amp;lt;data&amp;gt; HTTP POST binary data (H)
    --data-urlencode &amp;lt;name=data/name@filename&amp;gt; HTTP POST data url encoded (H)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We will use &lt;code&gt;--data-ascii&lt;/code&gt; as we are sending JSON data which is built of characters.&lt;/p&gt;

&lt;p&gt;By default curl reads it&#180;s data from stdin, which means we could either pipe the data into curl. But a slicker way would be to use the &lt;code&gt;@&lt;/code&gt; symbol together with a file name. This tells curl to read from a file instead of the content directly after the data flag.&lt;/p&gt;

&lt;p&gt;Together it looks like this &lt;code&gt;--data-ascii &quot;@filename.ext&quot;&lt;/code&gt; Without @, curl will just send &quot;filename.ext&quot; as request payload. Which is not what we  want.&lt;/p&gt;

&lt;p&gt;Now when we know about these two flags, let&#180;s prepare a customer.json file to send to our web service:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot; prettyprint lang-js&quot;&gt;{
   &quot;Name&quot;:&quot;Per Johansson&quot;,
   &quot;Profession&quot;: &quot;Hacker&quot;,
   &quot;Age&quot;: 26
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To send it we will put the two flags  together like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot; prettyprint&quot;&gt;curl -X POST --data-ascii &quot;@customer.json&quot; http://localhost/customers  
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This should work ok, but I forgot to tell you about another important flag. &lt;code&gt;-H&lt;/code&gt; which allow us to specify request headers.&lt;/p&gt;

&lt;p&gt;This is important because normally we should specify a suiting &lt;a href=&quot;http://en.wikipedia.org/wiki/Internet_media_type&quot;&gt;mime type&lt;/a&gt; for the stuff we are sending so that the server knows what to do with it.&lt;/p&gt;

&lt;p&gt;Here is the same thing again but with the right content type.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot; prettyprint&quot;&gt;curl -X POST -H &quot;Content-Type: application/json&quot; --data-ascii &quot;@customer.json&quot; http://localhost/customers  
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Cool right!?&lt;/p&gt;

&lt;p&gt;Finally if you know that the server will respond with json and you happen to have python installed you can pipe the response into &lt;code&gt;python -mjson.tool&lt;/code&gt;. This will pretty print the response so it is easily readible.&lt;/p&gt;

&lt;p&gt;And with that I think we&#39;re done. How to use curl to post json from disk to a webservice:  &lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint&quot;&gt;curl -X POST -H &quot;Content-Type: application/json&quot; --data-ascii &quot;@customer.json&quot; http://localhost/customers | python -mjson.tool  
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;alternatives&quot;&gt;Alternatives&lt;/h2&gt;

&lt;p&gt;If you don&#39;t like hacking in the terminal (cannot understand why anyone would not) there are some other things you can do. Like loading jQuery into your browser and using the browser console to fire off your requests. &lt;/p&gt;

&lt;p&gt;Or you could try plugins like these: &lt;br /&gt;
&lt;a href=&quot;https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn&quot;&gt;https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn&lt;/a&gt; &lt;br /&gt;
&lt;a href=&quot;https://addons.mozilla.org/en-US/firefox/addon/restclient/&quot;&gt;https://addons.mozilla.org/en-US/firefox/addon/restclient/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy requesting! &lt;br /&gt;
Per&lt;/p&gt;</description>            <guid>http://per.ghostify.io/using-curl-to-communicate-with-your-webservice/</guid>            <pubDate>Fri, 20 Dec 2013 11:43:39 GMT</pubDate>           <category>Blog post</category></item><item> <title>Typography test</title>            <link>http://per.ghostify.io/typography-test/</link>            <description>&lt;p&gt;One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.&lt;/p&gt;

&lt;p&gt;He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections.&lt;/p&gt;

&lt;p&gt;The bedding was hardly able to cover it and seemed ready to slide off any moment.&lt;/p&gt;

&lt;p&gt;His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;prettyprint lang-js&quot;&gt;while (lowerBoundIsLesserOrEqualToHigherBound()){  
    var middleBound = calculateNewMiddleBound();
    if(keyIsWithinLowerBound()){
        assignMiddleBoundToHigherBound();
    } else if(keyIsWithinHigherBound()){
        assignMiddleBoundToLowerBound();
    }else{
        return middleBound;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It wasn&#39;t a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls.&lt;/p&gt;

&lt;p&gt;A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame.&lt;/p&gt;

&lt;p&gt;It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather. Drops&lt;/p&gt;</description>            <guid>http://per.ghostify.io/typography-test/</guid>            <pubDate>Thu, 19 Dec 2013 22:14:21 GMT</pubDate>           <category>Blog post</category></item><item> <title>Hello world</title>            <link>http://per.ghostify.io/hello-brave-new-world/</link>            <description>&lt;p&gt;Trying out the new &lt;a href=&quot;https://ghost.org/&quot;&gt;ghost&lt;/a&gt; platform with hosting at &lt;a href=&quot;http://ghostify.io/&quot;&gt;ghostify&lt;/a&gt;. I am so far pretty happy with it. Perfect balance between simplicity and freedom.&lt;/p&gt;

&lt;p&gt;Anyway...&lt;/p&gt;

&lt;p&gt;More to come...&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;</description>            <guid>http://per.ghostify.io/hello-brave-new-world/</guid>            <pubDate>Thu, 19 Dec 2013 21:48:14 GMT</pubDate>           <category>Blog post</category></item></channel>
</rss>