How to test REST APIs using CURL
Curl is a command line tool that enables the transfer of data between various network protocols. Curl was first released in 1996 by a Swedish developer Daniel Stenberg.
Introduction
APIs are crucial in interfacing between the client and server. The most widely used type of API is Representational State Transfer (REST) API. They are conventionally easy to build hence their popularity among developers. REST APIs are stateless. Each request is processed in isolation and must contain all the necessary data for the request to be processed by the server.
Create a simple Rest API in GO
As a developer, while creating a rest API you are continually required to test the API for its functionality. This guide will cover different tools that can be used to test rest APIs and how to use them.
Testing REST APIs using CURL
Curl is a command line tool that enables the transfer of data between various network protocols. Curl was first released in 1996 by a Swedish developer Daniel Stenberg. Curl has a lot of use cases. Among them, you can use curl to test REST APIs.
CURL: GET request
Simply test the GET request like below.
The output should be the raw GET request response.
CURL: POST request
The command should post/add{"id":9,"title":"Post 1","data":"Lorem ipsum..."}
CURL: PUT request
PUT
is used to modify existing data. In this case, we are modifying the title to say post 9
instead of post 1
.
CURL: DELETE request
CURL: Authorization required
You should append - u
when authorization is required. Below is an example of GET request that requires authorization.
CURL: Adding headers to the request
We use -H
to add headers to the request. Below is an example.
CURL: Viewing headers / Debug CURL Request
We use -v
which stands for "verbose mode" to get more details about the headers in use. Below is an example:
Conclusion
With the above commands, we have covered the basics of curl
. CURL is useful when you want to quickly test an API or you are in an environment without a graphical user interface such as VPS. However, in development scenarios, you would require to use clients such as Insomnia or Postman.