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 ;))

POST

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

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

PUT

curl -i -H "Accept: application/xml" -X PUT -d "mail=niklas@mailinator.com" http://127.0.0.1/students/student/82  

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

DELETE

curl -i -H "Accept: application/xml" -X DELETE http://127.0.0.1/students/student/82  

This will delete student with id number 82

GET

curl -i -H "Accept: application/xml" -X GET "http://127.0.0.1/students?firstname=niklas&lastname=ottosson"  

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

OPTIONS
-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>