curl
Introduction
curl is a powerful command-line tool used for transferring data with URLs. It supports various protocols, including HTTP, HTTPS and FTP and has become an essential tool for developers, system administrators, and network engineers. It is widely used for testing APIs, automating web requests, and downloading files.
Practical curl examples
Before you begin
If you want to test the examples below and see the results in RequestBite's Inspector, then go to requestbite.com/view and click the "Copy URL" link. Then type the following into your terminal:
Replace the URL above with your own unique URL and run the commands below.
PATCH request with JSON data
PATCH requests are typically used to update partial resources. To send a PATCH
request with JSON data, use the -X PATCH
flag along with -H "Content-Type:
application/json"
to specify the request type and -d
to provide the data:
curl -X PATCH $URL \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Name",
"status": "active"
}'
Submitting regular form data
When submitting form data (application/x-www-form-urlencoded), use the -d
flag:
Submitting form with file
To upload a file using a form submission, use the -F
flag. Here’s an example
where we upload an image file (don't forget to replace the /path/to/image.jpg
to an actual file on your system):
Submitting binary payload from file
If you need to send raw binary data from a file, use the --data-binary
flag:
Common curl parameters
1. -v
(Verbose Mode)
This flag provides detailed information about the request and response, including headers:
2. -s
(Silent Mode)
Suppresses progress output and error messages, useful for scripting:
3. -o
(Output to File)
Redirects the response output to a file instead of displaying it in the terminal:
4. -L
(Follow Redirects)
Ensures curl follows HTTP redirects:
5. -H
(Custom Headers)
Allows adding custom headers to the request:
6. -u
(Basic Authentication)
For APIs requiring Basic Authentication: