DEV Community

Cover image for How to use curl
Chamal Randika
Chamal Randika

Posted on

How to use curl

# What is curl?

curl (connect URL) is a command line tool and a library for transferring data with URLs.

# Why you should learn curl?

Curl literay let's do anything related urls. It can:

  1. Test any REST/ GraphQL/ API
  2. Test anything else related to http/https requests and responses.
  3. Test xml/ json RPC protocols
  4. Upload/ download files
  5. Perform Monitoring and deployments with the help of scripts

If you're an engineer of just a pc enthusiast who works with urls, you should learn curl coz it's gonna make your life easier.

# Useful curl Flags:

  • -X <method>: Specifies the HTTP method (e.g., GET, POST, PUT, DELETE).
  • -H "<header>": Adds a header to the request.
  • -d "<data>": Sends data with a POST request (useful for sending JSON).
  • -I: Fetches only the HTTP headers.
  • -O: Saves the response to a file.
  • -L: Follows redirects.
  • -u <username>:<password>: For basic authentication.
  • -F "<form-field=@file>": Used to upload files.
  • -v: Enables verbose output to see request/response details.
  • -H "Authorization: Bearer <token>": Adds a Bearer token for authentication.

# Examples of Using curl

REST API Example

To make a GET request to a REST API endpoint:

curl -X GET https://api.example.com/users
Enter fullscreen mode Exit fullscreen mode

To send data with a POST request:

curl -X POST https://api.example.com/login \
-H "Content-Type: application/json" \
-d '{"username": "user", "password": "pass"}'
Enter fullscreen mode Exit fullscreen mode

GraphQL API Example

To send a query to a GraphQL API:

curl -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ users { id name } }"}'
Enter fullscreen mode Exit fullscreen mode

To use variables in the query:

curl -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-d '{"query": "query($id: ID!) { user(id: $id) { name } }", "variables": {"id": "123"}}'
Enter fullscreen mode Exit fullscreen mode

JSON-RPC Example

To call a method in a JSON-RPC API:

curl -X POST https://example.com/api \
-H "Content-Type: application/json" \
-d '{
  "jsonrpc": "2.0",
  "method": "getUser",
  "params": {"id": 1},
  "id": 1
}'
Enter fullscreen mode Exit fullscreen mode

File Upload Example

To upload a file using curl:

curl -X POST https://example.com/upload \
-F "file=@path/to/file.jpg"
Enter fullscreen mode Exit fullscreen mode

File Download Example

To download a file and save it locally:

curl -O https://example.com/file.zip
Enter fullscreen mode Exit fullscreen mode

Testing API with Authentication

For a REST API with Bearer token authentication:

curl -X GET https://api.example.com/protected-resource \
-H "Authorization: Bearer <your-token>"
Enter fullscreen mode Exit fullscreen mode

Testing API with Basic Authentication

For a REST API with Basic authentication:

curl -u "username:password" https://api.example.com/protected-resource
Enter fullscreen mode Exit fullscreen mode

Follow Redirects

To make a request and follow any redirects:

curl -L https://example.com
Enter fullscreen mode Exit fullscreen mode

Get Only HTTP Headers

To retrieve only the HTTP headers of a response:

curl -I https://example.com
Enter fullscreen mode Exit fullscreen mode

This should be all in one block, ready for you to use. Let me know if you need any more adjustments!
Enter fullscreen mode Exit fullscreen mode

# Curl is more than you think

Believe me when I say this, I cannot cover all of the curl's functionalities from one article.

If I did, it would be book. Infact, there is one if you're curious.

Go ahead and read if you want to know -> Everything about curl

Top comments (0)