Skip to content

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:

export URL=https://requestbite.com/req/XYZ

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:

curl -X POST $URL \
     -d "username=johndoe&password=securepass"

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):

curl -X POST $URL \
     -F "file=@/path/to/image.jpg" \
     -F "description=Profile picture"

Submitting binary payload from file

If you need to send raw binary data from a file, use the --data-binary flag:

curl -X POST $URL \
     --data-binary @/path/to/file.bin

Common curl parameters

1. -v (Verbose Mode)

This flag provides detailed information about the request and response, including headers:

curl -v "https://example.com"

2. -s (Silent Mode)

Suppresses progress output and error messages, useful for scripting:

curl -s "https://example.com" > output.html

3. -o (Output to File)

Redirects the response output to a file instead of displaying it in the terminal:

curl -o downloaded_file.zip "https://example.com/file.zip"

4. -L (Follow Redirects)

Ensures curl follows HTTP redirects:

curl -L "https://short.url/example"

5. -H (Custom Headers)

Allows adding custom headers to the request:

curl -H "Authorization: Bearer YOUR_TOKEN" "https://api.example.com"

6. -u (Basic Authentication)

For APIs requiring Basic Authentication:

curl -u username:password "https://api.example.com/protected"