cURL
Here’s a breakdown of how to use cURL for different HTTP request types, along with examples:
Basics of cURL
- cURL is a powerful command-line tool for transferring data and making network requests using various protocols, including HTTP.
- General Syntax:
curl [options] [URL]
GET Requests
- The default method when using cURL.
- Used to fetch data from a server.
Example (get all Pokemon)
curl -v http://localhost:3000/pokemonExample (get one Pokemon by ID)
curl -v http://localhost:3000/pokemon/1POST Requests
- Used to send data to a server (e.g., submitting forms, uploading data).
- Use the
-X POSToption. - Use the
-doption to specify the data being sent.
Example (form-like data)
curl -v -X POST -d "name=Charmander&type=Fire" http://localhost:3000/pokemonExample (JSON data)
curl -v -X POST -H "Content-Type: application/json" -d '{"name": "Squirtle", "type": "Water"}' http://localhost:3000/pokemonPUT Requests
- Used to update existing resources on a server.
- Use the
-X PUToption. - Use the
-doption to specify the updated data.
Example (form-like data)
curl -v -X PUT -d "type=Flying" http://localhost:3000/pokemon/2Example (JSON data)
curl -v -X PUT -H "Content-Type: application/json" -d '{"type": "Psychic"}' http://localhost:3000/pokemon/2DELETE Requests
- Used to delete a resource on a server.
- Use the
-X DELETEoption.
Example
curl -v -X DELETE http://localhost:3000/pokemon/1Additional Notes
- Verbose Output: Add the
-voption to see detailed information about the request and response. - cURL’s power extends far beyond these basics. Check out the manual (
man curl) or online resources for features like file uploads, authentication, and more.
Important Considerations
- The server receiving your requests needs to be set up to handle the respective HTTP method you’re using.
- Pay attention to data formats (e.g., JSON, form-encoded) and the server’s requirements.