Using cURL to test a REST service

When testing a REST service I tend to use cURL most of the time. It’s fast and pretty easy (when you write down all the options like I have here ;))


curl -i -H "Accept: application/xml" -X POST -d "firstname=niklas&lastname=ottosson"  

This will create (POST) a new student with firstname ‘niklas’ and lastname ‘ottosson’


curl -i -H "Accept: application/xml" -X PUT -d ""  

This will set (PUT) the mail address on student with id number 82


curl -i -H "Accept: application/xml" -X DELETE  

This will delete student with id number 82


curl -i -H "Accept: application/xml" -X GET ""  

This will get us the student were firstname is ‘niklas’ and lastname ‘ottosson’ (quotes are needed for passing multiple parameters)

-i – tells cURL to display the response header so we can examine it for errors
-H – tells cURL to send request headers. In this example we tell the system that we only want xml formated responses (“Accept: application/xml”)
-X – sets what REST function we want to use
-d “data1=foo&data2=bar” – is the data we want to send/save (only for PUT or POST)

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.