Testing RESTful APIs with Curl

$ curl -I http://localhost:8080/
$ curl --head http://localhost:8080/

GET

Optionally include -i or --include to show the response header

$ curl -i http://localhost:8080/
$ curl --include http://localhost:8080/

Get a json response:

$ curl -H "Accept:application/json" http://localhost:8080/
$ curl -H "Accept:application/json" -H "Accept-encoding:gzip" http://localhost:8080/

Pretty JSON:

$ curl http://localhost:8080/ | python -m json.tool

POST

$ curl -X POST -H "Accept:application/json" -H "Accept-encoding:gzip" http://localhost:8080/ -d '{"hello": "world"}'
$ curl --request POST -H "Accept:application/json" -H "Accept-encoding:gzip" http://localhost:8080/ -d '{"hello": "world"}'

Cookies

$ curl 'http://localhost:8080/' -H 'Cookie: first_cookie=first_value; second_cookie=second_value'

Including a URI encoded date/time

This will be submitted with a content-type of application/x-www-form-urlencoded:

$ curl -X POST http://localhost:8080/ --data-urlencode time=`date '+%FT%T%z'`

File Upload

$ curl -X POST http://localhost:8080/ --form "data=@/path/to/file"

PUT

$ curl -X PUT -H "Accept:application/json" -H "Accept-encoding:gzip" http://localhost:8080/ -d '{"hello": "world"}'
$ curl --request PUT -H "Accept:application/json" -H "Accept-encoding:gzip" http://localhost:8080/ -d '{"hello": "world"}'

DELETE

$ curl -X DELETE -H "Accept:application/json" -H "Accept-encoding:gzip" http://localhost:8080/ -d '{"hello": "world"}'
$ curl --request DELETE -H "Accept:application/json" -H "Accept-encoding:gzip" http://localhost:8080/ -d '{"hello": "world"}'

Disabling keep-alive

Use the --no-keepalive option.

-- Frank Dean - 8 Feb 2016