REST Services cURL cheatsheet

Explore this comprehensive cheatsheet tailored for REST developers seeking a powerful yet straightforward testing tool for their REST services. Discover essential commands and options that elevate your testing capabilities using a versatile tool like curl. Whether you’re a seasoned developer or just getting started, this cheatsheet provides valuable insights to streamline your REST API testing workflow.

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 50 POST Request in Parallel

 seq 1 50 | xargs -n 1 -P 50 curl -d '{"name":"John","email":"[email protected]","phoneNumber":"1234567890"}' -H "Content-Type: application/json" -X POST http://localhost:8080/kitchensink/rest/members

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

REST Services uploading and downloading Files

Uploading a File to the REST Service:

curl -F [email protected] https://example.com/upload

How to download a File:

curl -o myfile.txt https://example.com/download.zip

For a more comprehensive tutorial about uploading and downloading Files, check this article: How to Upload and Download Files with a Servlet

Conclusion

In conclusion, mastering the art of REST API testing is crucial for every developer, and having a reliable tool like curl at your disposal can greatly enhance your testing capabilities. This cheatsheet has provided a valuable reference, covering essential commands and options to simplify the testing process. As you incorporate these techniques into your workflow, you’ll find yourself navigating the world of REST services with increased efficiency and confidence. Keep exploring, testing, and refining your skills to ensure seamless and robust interactions with REST APIs in your development journey.