cURL Guide for HTTP Methods
cURL is a command-line tool used to interact with APIs and perform HTTP requests. The name cURL is derived from "Client for URLs" or simply "command URL". Below is a guide on how to use cURL for common HTTP methods.
1. GET Request
Used to retrieve data from a server.
curl -X GET https://api.example.com/resource
With Query Parameters:
curl -X GET "https://api.example.com/resource?key=value&key2=value2"
With Headers:
curl -X GET https://api.example.com/resource \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json"
You can use curl without the -X option. By default, curl uses the GET method if no HTTP method is specified.
curl http://localhost:11434/api/models
This will perform a GET request to the specified URL.
2. POST Request
Used to send data to a server to create a resource.
curl -X POST https://api.example.com/resource \
-d '{"key":"value", "key2":"value2"}' \
-H "Content-Type: application/json"
With Form Data:
curl -X POST https://api.example.com/resource \
-F "key=value" \
-F "file=@/path/to/file.txt"
With Headers:
curl -X POST https://api.example.com/resource \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"key":"value"}'
You can omit -X POST if you include data using the -d option, as curl automatically switches to POST when data is provided:
curl http://localhost:11434/api/generate \
-d '{ "model": "llama3.2", "prompt": "Tell me a fun fact about space." }'
3. PUT Request
Used to update an existing resource.
curl -X PUT https://api.example.com/resource/123 \
-d '{"key":"new_value"}' \
-H "Content-Type: application/json"
With Headers:
curl -X PUT https://api.example.com/resource/123 \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"key":"updated_value"}'
4. DELETE Request
Used to delete a resource.
curl -X DELETE https://api.example.com/resource/123
With Headers:
curl -X DELETE https://api.example.com/resource/123 \
-H "Authorization: Bearer YOUR_TOKEN"
For methods like DELETE, you must explicitly specify the method using -X because curl does not assume DELETE by default
5. PATCH Request
Used to partially update a resource.
curl -X PATCH https://api.example.com/resource/123 \
-d '{"key":"partial_update"}' \
-H "Content-Type: application/json"
6. OPTIONS Request
Used to retrieve supported HTTP methods for a resource.
curl -X OPTIONS https://api.example.com/resource
7. HEAD Request
Used to retrieve headers without the response body.
curl -X HEAD https://api.example.com/resource
8. Sending Authentication
Basic Authentication:
curl -u username:password https://api.example.com/resource
Bearer Token:
curl -X GET https://api.example.com/resource \
-H "Authorization: Bearer YOUR_TOKEN"
9. Saving Response to a File
curl -X GET https://api.example.com/resource -o response.json
10. Verbose Mode (Debugging)
curl -X GET https://api.example.com/resource -v
11. Custom Headers
curl -X GET https://api.example.com/resource \
-H "Custom-Header: CustomValue"
12. Follow Redirects
curl -L https://api.example.com/resource
13. Sending Data from a File
curl -X POST https://api.example.com/resource \
-d @data.json \
-H "Content-Type: application/json"
14. Combining Multiple Options
curl -X POST https://api.example.com/resource \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"key":"value"}' \
-o response.json -v
Summary
- GET: No need for
-X, as it's the default method. - POST: Automatically used when
-dor--datais provided. - Other Methods (PUT, DELETE, etc.): Require
-Xto specify the method explicitly.
