curl
is a versatile command-line tool used to transfer data from or to a server. It supports various protocols, including HTTP, HTTPS, FTP, and many more. One of the most common uses of curl
is to test API endpoints during development. In this blog post, we’ll explore how to use curl
to interact with APIs, covering common scenarios such as GET, POST, PUT, DELETE requests, and more.
Why Use curl
?
curl
is an essential tool for developers because it:
- Provides a simple and efficient way to make HTTP requests.
- Works on most operating systems.
- Supports a wide range of protocols.
- Allows for easy debugging of network issues.
- Is scriptable, making it easy to automate tasks.
Basic curl
Commands
Sending a GET Request
A GET request retrieves data from an API endpoint. The syntax is straightforward:
curl https://api.example.com/resource
To include headers in the request, use the -H
option:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/resource
Sending a POST Request
A POST request is used to submit data to an API endpoint. Use the -d
option to include data:
curl -X POST -d "param1=value1¶m2=value2" https://api.example.com/resource
For JSON data, use the -H
option to set the Content-Type
header:
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' https://api.example.com/resource
Sending a PUT Request
A PUT request updates data at an API endpoint. The syntax is similar to a POST request:
curl -X PUT -H "Content-Type: application/json" -d '{"key1":"updated_value1", "key2":"updated_value2"}' https://api.example.com/resource/1
Sending a DELETE Request
A DELETE request removes data from an API endpoint. Simply specify the endpoint:
curl -X DELETE https://api.example.com/resource/1
Advanced curl
Usage
Including Headers
To include multiple headers, use the -H
option multiple times:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -H "Content-Type: application/json" https://api.example.com/resource
Saving Response to a File
Use the -o
option to save the response to a file:
curl https://api.example.com/resource -o response.json
Viewing Detailed Output
Use the -v
option to view detailed output, including request and response headers:
curl -v https://api.example.com/resource
Handling Cookies
Use the -c
option to save cookies and the -b
option to send cookies:
curl -c cookies.txt -b cookies.txt https://api.example.com/resource
Following Redirects
Use the -L
option to follow redirects:
curl -L https://api.example.com/resource
Specifying a Request Timeout
Use the --max-time
option to specify a request timeout in seconds:
curl --max-time 10 https://api.example.com/resource
Practical Examples
Example 1: Authenticating with an API
First, obtain an access token using a POST request:
curl -X POST -H "Content-Type: application/json" -d '{"username":"user", "password":"pass"}' https://api.example.com/authenticate
Then, use the token in a GET request:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/protected-resource
Example 2: Uploading a File
To upload a file using a POST request, use the -F
option:
curl -X POST -F "file=@/path/to/file.txt" https://api.example.com/upload
Example 3: Sending Form Data
Send form data using the -d
option:
curl -X POST -d "name=John&age=30" https://api.example.com/form
Example 4: Fetching JSON Data
Fetch JSON data and pretty-print it using jq
:
curl -s https://api.example.com/resource | jq .
Conclusion
curl
is an incredibly powerful tool for testing and interacting with API endpoints. Whether you’re fetching data, submitting forms, uploading files, or debugging network issues, curl
offers a simple yet flexible way to perform a wide range of HTTP requests. By mastering the basics and exploring advanced options, you can greatly enhance your productivity and efficiency in API development.