Tuesday, March 18, 2008

How to HTTP POST a request using cURL.

I had to test an api we did the other day for an external company.
We agreed on only allowing POST requests to the api URL.

So.. how do i go ahead and test it then ?!
Using curl to post data into a certail url.

(taken from http://curl.haxx.se/docs/httpscripting.html)
4.2 POST
 
The GET method makes all input field names get displayed in the URL field of
your browser. That's generally a good thing when you want to be able to
bookmark that page with your given data, but it is an obvious disadvantage
if you entered secret information in one of the fields or if there are a
large amount of fields creating a very long and unreadable URL.
 
The HTTP protocol then offers the POST method. This way the client sends the
data separated from the URL and thus you won't see any of it in the URL
address field.
 
The form would look very similar to the previous one:
 
<form method="POST" action="junk.cgi">
<input type=text name="birthyear">
<input type=submit name=press value=" OK ">
</form>
 
And to use curl to post this form with the same data filled in as before, we
could do it like:
 
curl -d "birthyear=1905&press=%20OK%20" www.hotmail.com/when/junk.cgi
 
This kind of POST will use the Content-Type
application/x-www-form-urlencoded and is the most widely used POST kind.
 
The data you send to the server MUST already be properly encoded, curl will
not do that for you. For example, if you want the data to contain a space,
you need to replace that space with %20 etc. Failing to comply with this
will most likely cause your data to be received wrongly and messed up.
 

:popular_tags => [ruby, rails, ruby-on-rails, רובי-און-ריילס, console,,tricks, youtube, links, screeshots, toturials],
:email_me => 'shmuel@ahdut.com',
:subscribe_to_rss => ,
:sites => [pawst.com, urlazy.com],
:sponsored_by =>