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.

How to test REST APIs using CURL
curl

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.

curl https://example.com/post/1 
curl GET example

The output should be the raw GET request response.

CURL: POST request

curl -d '{"id":9,"title":"Post 1","data":"Lorem ipsum..."}' -H 'Content-Type: application/json' -X POST https://example.com/post/submit
curl POST example

The command should post/add{"id":9,"title":"Post 1","data":"Lorem ipsum..."}

CURL: PUT request

curl -d '{"id":9,"title":"Post 9","data":"Lorem ipsum..."}' -H 'Content-Type: application/json' -X PUT https://example.com/post/submit
curl PUT example

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 -X DELETE https://example.com/post/9/delete/1
curl DELETE example

CURL: Authorization required

You should  append - u when authorization is required. Below is an example of GET request that requires authorization.

curl -u username:password https://example.com/post/9
curl with authorization

CURL: Adding headers to the request

We use -H to add headers to the request. Below is an example.

curl -H "Content-Type: application/json" -H "User-Agent: UserAgentString" https://www.example.com
curl with headers

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:

curl -v https://example.com
curl verbose

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.

Subscribe to Bluedoa Digest

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
alexander@example.com
Subscribe