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: Test any REST/ GraphQL/ API Test anything else related to http/https requests and responses. Test xml/ json RPC protocols Upload/ download files 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 : Specifies the HTTP method (e.g., GET, POST, PUT, DELETE). -H "": Adds a header to the request. -d "": 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 :: For basic authentication. -F "": Used to upload files. -v: Enables verbose output to see request/response details. -H "Authorization: Bearer ": 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 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"}' 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 } }"}' 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"}}' 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 }' File Upload Example To upload a file using curl: curl -X POST https://example.com/upload \ -F "file=@path/to/file.jpg" File Download Example To download a file and save it locally: curl -O https://example.com/file.zip Testing API with Authentication For a REST API with Bearer token authentication: curl -X GET https://api.example.com/protected-resource \ -H "Authorization: Bearer " Testing API with Basic Authentication For a REST API with Basic authentication: curl -u "username:password" https://api.example.com/protected-resource Follow Redirects To make a request and follow any redirects: curl -L https://example.com Get Only HTTP Headers To retrieve only the HTTP headers of a response: curl -I https://example.com This should be all in one block, ready for you to use. Let me know if you need any more adjustments! # 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

May 3, 2025 - 17:11
 0
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 : Specifies the HTTP method (e.g., GET, POST, PUT, DELETE).
  • -H "
    ": Adds a header to the request.
  • -d "": 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 :: For basic authentication.
  • -F "": Used to upload files.
  • -v: Enables verbose output to see request/response details.
  • -H "Authorization: Bearer ": 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

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"}'

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 } }"}'

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"}}'

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
}'

File Upload Example

To upload a file using curl:

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

File Download Example

To download a file and save it locally:

curl -O https://example.com/file.zip

Testing API with Authentication

For a REST API with Bearer token authentication:

curl -X GET https://api.example.com/protected-resource \
-H "Authorization: Bearer "

Testing API with Basic Authentication

For a REST API with Basic authentication:

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

Follow Redirects

To make a request and follow any redirects:

curl -L https://example.com

Get Only HTTP Headers

To retrieve only the HTTP headers of a response:

curl -I https://example.com

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

# 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