CURL for dummies

CURL for dummies

After being in the industry for so many years, I always felt that the power of curl is something which was unexplored by me. Most of the time I end up using Postman. Though Postman is a great tool with all the curl features implemented, but there are few scenarios where I felt my lack of knowledge of curl stopped me from moving faster. One such instance was in my previous job I was asked to fix something in an On-Premise system which was production critical and after initial round of debugging we came to understand that firewall was blocking outgoing requests. I ended up writing python script to perform few basic http operations which was pretty easy to be done using curl. Lets look into basic curl operation

How to make GET request using curl?

By default curl makes http get method request.


The response in the above request is going be simple html response code which the browser receives when a request is made to open If no http method is mentioned then curl would be using GET request.

Here is the syntax for curl

curl [options] [url]

Following are the options which we use day to day

  • curl -o quill_editor.js would download  the content of the quill js and save it in a new file named quill_editor.js
  • curl -O  would download the content of quill js and save it in quill.js
  • curl -I --http2 would return only the http header response
  • curl -L would automatically do http redirects.
  • curl  -A "GoogleBot" would update the UserAgent to googlebot rather than the default curl UserAgent
  • curl -L -b "newCookie=a" would set the cookie newCookie=a in the http header request before sending.

How to do POST request using curl?

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"data":"post_data", "num":10}' http://localhost:8000/api/postJsonRequest

If you want to send a larger request body then it is difficult to send via command line and would be recommended to save the content to a file and then use the following command

curl -H "Content-Type: application/json" -X POST --data @body.json http://localhost:8000/postrequest/

Here the post request body is saved in body.json also do make sure the @ character added before the file name, this is very important.

Similarly for any PUT/PATCH request all we need to do is update the method,

curl -H "Content-Type: application/json" -X PUT --data @body.json http://localhost:8000/postrequest/