Here is a cheatsheet that might be useful for REST developers that need a simple tool like curl for testing their REST services.

How to execute a POST application/x-www-form-urlencoded

As application/x-www-form-urlencoded is the default, you can just execute:

$ curl -d "param1=value1&param2=value2" -X POST http://localhost:8080/service

If you want to use explicit setting:

$ curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:8080/service

How to send parameters contained in a file

curl -d "@data.txt" -X POST http://localhost:8080/service

Where data.txt might contain, for example:

param1=value1&param2=value2

How to execute a POST application/json

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:8080/service

How to execute a POST application/json from a file:

curl -d "@data.json" -X POST http://localhost:8080/service

How to execute a POST request with form parameters:

$ curl -X PUT -d 'param1=value1&param2=value2' http://localhost:8080/service

How to execute a POST request with Json parameters:

$ curl -X PUT -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://localhost:8080/service

How to execute an HEAD request:

curl -I http://localhost:8080/service

How to execute a DELETE request:

curl -X "DELETE" http://www.url.com/page

How to execute a non-proxy request:

curl --noproxy 127.0.0.1 http://localhost:8080/service

How to execute a GET with extra Headers:

For example, let's set json as Content type:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:8080/service

Or let's set XML as Content type:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://localhost:8080/service

How to execute a GET with credentials:

curl -u $username:$password http://localhost:8080/service
0
0
0
s2sdefault