Test your APIs with cURL➰

Test your APIs with cURL➰

Have you ever made an API? Maybe it was a REST or a GraphQL API. Now you need to test it. But how? Postman, or any other tool to check your endpoints? Is there a simpler way? Yes of course, and you don't even have to leave your command line!

Hey there. In this tutorial, we will learn about cURL, which is a command line tool to test your APIs. It is one of the best things I found while learning web development, and this is a great tutorial to get started with this amazing tool. So let's get going.

Why cURL?

Because it is way easier to write a command than to go to an app and paste your url endpoint and do a GET or POST or whatever request you need.
But this is not the only advantage. You can do a lot of things with cURL, such as -

  • Use different protocols like FTP, TELNET, HTTPS,etc. Default protocol is HTTP.
  • Requesting data from endpoints and storing output in a file, FTP uploads, SSL connections, and so on. So now you know that this tool is very powerful and handy once you get the hang of it.

Basic commands

cURL comes pre-installed with Windows 10, and also on Mac or Linux. If it isn't on your system, you can go here to download it. We will use a fake API(JSON placeholder) for demonstrating cURL commands.

GET Request

curl https://jsonplaceholder.typicode.com/posts

This will get all data on this endpoint. If you want to return only the HTTP headers for the endpoint, use "-I" flag -

curl -I https://jsonplaceholder.typicode.com/posts

will return

HTTP/2 200
date: Fri, 15 Jan 2021 12:25:43 GMT
content-type: application/json; charset=utf-8
set-cookie: __cfduid=d8a587b28cd61309a617e2e924bdbfead1610713543; expires=Sun, 14-Feb-21 12:25:43 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax
x-powered-by: Express
x-ratelimit-limit: 1000
x-ratelimit-remaining: 999
x-ratelimit-reset: 1608078297
vary: Origin, Accept-Encoding
access-control-allow-credentials: true
cache-control: max-age=43200
pragma: no-cache
expires: -1
x-content-type-options: nosniff
etag: W/"6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM"
via: 1.1 vegur
cf-cache-status: HIT
age: 10162
cf-request-id: 07a79b4c740000d78951b46000000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=vDoGj9EZAh2VYtluNOa6lIASA5kGXZvRW%2FJRWdwubZI2Lrp2k3CYb%2F6udpMac4f4%2FVJMinD3FgY3diNbA16VY1FAKrGwzXLuOY8UeeZg9CA3u5bBWJCPc6ds0ay73ZtHnlzkEtvElE%2FU"}],"group":"cf-nel","max_age":604800}
nel: {"report_to":"cf-nel","max_age":604800}
server: cloudflare
cf-ray: 611f94c0bee7d789-MRS

The lowercase -i flag will return headers with data, and uppercase -I will return only headers.

To save the output in a file, use the "-o" or "-O" flag. Difference between the two is, while using "-o" we will provide a predefined name for the file like "test.txt" , whereas in "-O", the file will be saved as it is on the endpoint -

curl -O https://jsonplaceholder.typicode.com/posts

or

curl -o test.txt https://jsonplaceholder.typicode.com/posts

POST Request

We can make a POST request like this -

curl -X POST https://jsonplaceholder.typicode.com/posts

"-X" flag is used for custom request method. But we aren't posting any data! Let's do that :

curl -d '{json}' -H 'Content-Type: application/json' https://jsonplaceholder.typicode.com/posts

"-d" flag is used to send data, "-H" flag is used to specify the type of data we are sending.

DELETE and PUT( update ) Requests

They are fairly simple -

curl -X DELETE https://jsonplaceholder.typicode.com/users/1

and

curl -X PUT -H "Content-Type: application/json" -d '{"name":"mkyong","email":"abc@gmail.com"}' https://jsonplaceholder.typicode.com/users/1

Formatting

While writing these commands, the readability reduces as we try to do more with our testing. One way to format is to put backslashes -

curl -X POST \
  "https://jsonplaceholder.typicode.com/users" \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  -d '
  {
    "email": "frog@example.com",
    "password": "fishfish"
  }'

There you go! A simple introduction to cURL. If you are a beginner in API development, this tool is a great choice for testing your APIs. It's super powerful, and verbose. For further reading, see cURL's docs

Photo by Ylanite Koppens from Pexels