What is curl?
curl (connect URL) is a command line tool and a library for transferring data with URLs.
Why you should learn curl?
- 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 <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
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 <your-token>"
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
Top comments (0)