When building APIs, you need a way to send requests to test out what you are building. For big projects, tools like Postman or Insomnia are a great choice because they offer much more than just a way to send requests.
However, for small projects where all you need is a way to send requests to test your API, I find using cURL much more convenient. Not only that, but you also look cool, and who doesn't want that.
If not specified differently, default HTTP method is GET.
curl [url]
curl http://localhost:8000/api/v1/todos
To specify different HTTP methid use the -X
option.
curl -X DELETE [url]
curl -X DELETE http://localhost:8000/api/v1/todos/1
For sending requests with data, use -d
flag.
curl -X POST -d @data.json http://localhost:8000/api/v1/todos
curl -X PUT -d @data.json http://localhost:8000/api/v1/todos
curl -X PATCH -d @data.json http://localhost:8000/api/v1/todos
{ "title": "foo", "body": "bar", "userId": 1 }
If you are using -d
flag, request is by default treated as POST request, so you can omit the
-X
option.
curl -d @data.json http://localhost:8000/api/v1/todos
When using query parameters, wrap the url in ""
to avoid potential problems.
curl "http://localhost:8000/api/v1/todos?id=1"
To send request headers use -H
option.
curl -H "Authorization: Bearer <token>" http://localhost:8000/api/v1/todos
To see information such as response status and response headers, use -i
flag.
curl -i http://localhost:8000/api/v1/todos
When you need to send a request with multiple headers and a long url, it can quickly become a burden to type everything. You can easily end up with something like this:
curl -i -X POST -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d @data.json http://localhost:8000/api/v1/todos
To avoid that, I recommend using a cli tool like endpoint.