RESTful testing with cURL

POST data to a REST resource:
curl -i -H "Accept: application/json" -X POST -d "firstName=james" http://resturl/section/sub

i – show response headers
H – pass request headers to the resource
X – pass a HTTP method name
d – pass in parameters enclosed in quotes; multiple parameters are separated by ‘&’
The above command posts the first name “james” to the persons resource. Assuming the server creates a new person resource with first name of James, I also tell the server to return a json representation of the newly created resource.


PUT a resource:
curl -i -H "Accept: application/json" -X PUT -d "phone=1-800-999-9999" http://resturl/persons/person/1

This puts a phone number to the person resource created in the previous example.


GET a resource:
curl -i -H "Accept: application/json" http://resturl/persons/person/1

For GET requests, the -X GET option is optional.

curl -i -H "Accept: application/json" http://resturl/persons?zipcode=93031

You can pass in query parameters by appending it to the url.

curl -i -H "Accept: application/json" "http://resturl/persons?firstName=james&lastName=wallis"
The resource uri needs to be quoted if you pass in multiple query parameters separated by ‘&’. If you have spaces in the query values, you should encode them i.e. either use the ‘+’ symbol or %20 instead of the space.


DELETE a resource:

curl -i -H "Accept: application/json" -X DELETE http://resturl/persons/person/1
To delete a resource, supply DELETE as a -X option.

Using POST to PUT a resource:

curl -i -H "Accept: application/json" -H "X-HTTP-Method-Override: PUT" -X POST -d "phone=1-800-999-9999" http://resturl/persons/person/1

Some clients do not support PUT or it’s difficult to send in a PUT request. For these cases, you could POST the request with a request header of X-HTTP-Method-Override set to PUT. What this tells the server is that the intended request is a PUT.
Most web servers (or you could code it) support the X-HTTP-Method-Override and convert the request method to the intended HTTP method (value of the X-HTTP-Method-Override)
This example puts a phone number (by POSTing) to the person resource identified by 1.


Using POST to DELETE a resource:

curl -i -H "Accept: application/json" -H "X-HTTP-Method-Override: DELETE" -X POST http://resturl/persons/person/1

Similar to the previous command, this example deletes the person resource identified by the above uri using the POST HTTP method but telling the server to override it with DELETE.


เพิ่มเติม REST-esting with cURL: File Handling

ที่มา REST-esting with cURL