cURL is a command line interface tool (for Linux) that allows you to request and transfer data over a URL under different protocols. It is flexible and gives you the control of URLs in your terminal. Finally, executing cURL on the terminal is simple, but may not be intuitive to use by every user.
Table of contents
- What is cURL and how does it work?
- How to send GET requests?
- How to send POST requests?
- How to send PUT requests?
- How to send DELETE requests?
- How to send HEAD requests?
- How to upload files using cURL?
- Conclusion
What is cURL and how does it work?
You can use this software to execute HTTP requests. It is a command-line tool that lets you transmit HTTP requests and receive responses in your terminal or program. It is available for Linux distributions, Mac OS X, and Windows. To use the software to run your REST web API call, use the command syntax.
Also, this software is used in command lines or scripts to transfer data and it is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, medical devices, settop boxes, computer games, media players and is the Internet transfer engine for thousands of software applications in over twenty billion installations.
The most recent stable version is 8.4.0, released on 2023-10-11. Currently, 112 of the listed downloads are of the latest version.
Check out the latest source code from GitHub.
How to send GET requests?
Sending a GET HTTP request with this software is fairly simple. And, by default, the index of the website will be displayed in your terminal, but you can pipe the output into a .html file to re-use it afterwards.
Simple GET HTTP Request with cURL
The simplest usage form to execute a GET request to a website online, or locally for that matter:
# Execute a GET request and print in terminal
$ curl -X GET https://freeopensourc.es/feed/
# Execute a GET request and save to file
$ curl -X GET https://freeopensourc.es/feed/ > feed.xml
# Execute a GET request with parameters
$ curl -X GET https://example.com?this=is&a=list&of=parameters
Next we will tell the software that we want it to be verbose and display a lot more information about the GET request being executed. Also, we will use the -L flag to enable to follow HTTP redirects.
# Display connection information
$ curl -v -X GET https://freeopensourc.es/feed/ > feed.xml
# Instruct cURL to follow redirects
$ curl -L -X GET https://freeopensourc.es/feed/ > feed.xml
How to send POST requests?
Sending a POST HTTP request with this software is not too complicated either. Note that POST requests often require some parameters to be added and headers may be needed on the request as well.
Simple POST HTTP Request with cURL
The simplest usage form to execute a POST request to a website online, or locally for that matter:
# Execute a POST request and print in terminal
$ curl -X POST https://freeopensourc.es/feed/
# Execute a POST request and save to file
$ curl -X GET https://freeopensourc.es/feed/ > feed.xml
# Execute a POST request with headers
$ curl -X GET https://example.com -H 'Accept: application/json'
Likewise, we will now tell the software about some request parameters, for example to execute a login operation with the correct credentials.
# Execute a POST request with parameters in JSON format
$ curl -X POST https://example.com/submit \
-H 'Content-Type: application/json' \
-d '{"email": "[email protected]", "password": "NotARealPassword"}'
# Execute a POST request with URL-encoded data (form submit)
$ curl -X POST https://example.com/submit \
-H 'application/x-www-form-urlencoded' \
-d 'name=Free%20Open%20Sources'
How to send PUT requests?
Sending a PUT HTTP request with this software is next on the line. Note that PUT requests often require some parameters to be added and headers may be needed on the request as well.
Simple PUT HTTP Request with cURL
The simplest usage form to execute a PUT request to a website online, or locally for that matter:
# Execute a PUT request and print in terminal
$ curl -X PUT https://example.com/posts/123/update
# Execute a PUT request and save to file
$ curl -X PUT https://example.com/posts/123/update > feed.xml
# Execute a PUT request with headers and parameters
$ curl -X PUT https://example.com/posts/123/update
-H 'Content-Type: application/json' \
-d '{"title": "New Awesome Post Title", "slug": "new-awesome-post-title"}'
How to send DELETE requests?
Sending a DELETE HTTP request with this software now. Note that DELETE requests may be dangerous depending on the API that you use as they are typically put in place to remove entities in REST APIs. Call DELETE API endpoints only with caution as they may cause losses of data.
Simple DELETE HTTP Request with cURL
The simplest usage form to execute a DELETE request to a website online, or locally for that matter:
# Execute a DELETE request and print in terminal
$ curl -X DELETE https://example.com/posts/123
# Execute a DELETE request and save to file
$ curl -X DELETE https://example.com/posts/123 > feed.xml
# Execute a DELETE request with headers and parameters
$ curl -X DELETE https://example.com/posts/delete
-H 'Content-Type: application/json' \
-d '{"slug": "new-awesome-post-title"}'
How to send HEAD requests?
Next, let’s have a look at sending a HEAD HTTP request with this software and whether it is also simple. By default, the information about the HTTP protocol and available JSON APIs and server information are displayed using HEAD requests.
Simple HEAD HTTP Request with cURL
The simplest usage form to execute a HEAD request to a website online, or locally for that matter:
# Execute a HEAD request and print in terminal
$ curl --head https://example.com
# Execute a HEAD request and save to file
$ curl --head https://example.com > head
How to upload files using cURL?
Uploading a file with a HTTP request with this software is done using the form option. Under the hood, it uses the enctype multipart/form-data and attaches a file as specified in parameters.
Simple file upload with cURL
The simplest usage form to upload a file to a website online, or locally for that matter:
# Execute a file upload with cURL
$ curl -F 'fileA=@/path/to/fileA' https://example.com/posts/submit
# Execute multiple file uploads with cURL
$ curl https://example.com/posts/submit \
-F 'fileA=@/path/to/fileA' \
-F 'fileB=@/path/to/fileB' \
-F 'fileC=@/path/to/fileC' \
Conclusion
Moreover, in all user surveys and during all curl’s lifetime, HTTP has been the most important and most frequently used protocol that it supports. This will mostly work the same way for HTTPS, as they are really the same thing under the hood, as HTTPS is HTTP with an extra security TLS layer.
Find the most useful cheatsheet for commands here.
I hope this article was insightful for you and that the commands listed in this article helped you with your endeavours with cURL. Obviously, our team has been a fan of this software for the past decade and surely wouldn’t be reporting from the front if it weren’t for this amazing command line tool.
[…] promise-based architecture. Most prominent HTTP requests are GET, POST and PUT, and we have seen in here how you can execute these with cURL inside your terminal. Now it’s time to execute these […]