DEV Community

Cover image for The minimum you need to know to test your APIs with CURL
Douglas Moura
Douglas Moura

Posted on • Originally published at douglasmoura.dev

The minimum you need to know to test your APIs with CURL

CURL is a command-line tool that allows you to transmit data with URL syntax, supporting a myriad of protocols (DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP, WS and WSS). In this article, I will focus on using CURL to make HTTP requests to APIs, which, at least for me, is the most common use.

Installation

CURL is already installed on most Linux distributions and recent versions of Windows. To check if it's installed, just run the curl command in the terminal. If you don't have CURL installed, you can install it with the command sudo apt install curl (Ubuntu/Debian) or sudo yum install curl (CentOS/Fedora) or winget install curl (Windows).

Oh, and as it is common for us to work with REST APIs in web development, another command-line tool that will be useful to us is jq, which serves to format JSON in the terminal. To install jq, just run the command sudo apt install jq (Ubuntu/Debian) or sudo yum install jq (CentOS/Fedora) or winget install jqlang.jq (Windows).

Our example API

For didactic purposes, I will use DummyJSON as an API.

Making a GET request

To make a GET request, just run the curl command followed by the URL you want to access. For example, to request data for product 1, just run the command curl https://dummyjson.com/products/1.

And, to format the output, just add a | jq at the end of the command:

curl https://dummyjson.com/products/1 | jq
Enter fullscreen mode Exit fullscreen mode

Making a POST, PUT, PATCH or DELETE request with JSON in the body

To make a POST request, just run:

curl --json '{"title": "New product"}' https://dummyjson.com/products/add
Enter fullscreen mode Exit fullscreen mode

The curl will take care of adding the headers Content-Type: application/json and Accept: application/json. If you want to make a PUT, PATCH or DELETE request, add the -X option followed by the HTTP method you want to use. For example, to make a PUT, run:

curl -X PUT --json '{"title": "New title"}' https://dummyjson.com/products/1
Enter fullscreen mode Exit fullscreen mode

You can also send a JSON file instead of typing the JSON in the terminal by putting an @ in front of the file name:

curl --json @arquivo.json https://dummyjson.com/products/add
Enter fullscreen mode Exit fullscreen mode

Or passing data from stdin (note that I use @- instead of @ to indicate that the data will come from stdin):

curl --json @- https://dummyjson.com/products/add < file.json
Enter fullscreen mode Exit fullscreen mode

Making a request with headers

To make a request with headers, just run the curl command followed by the URL you want to access, and the -H option followed by the header you want to send. So, to send a Bearer Token, you would run the following command:

curl -H "Authentication: Bearer token" --json '{"title": "New product"}' https://dummyjson.com/products/add
Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)