---
title: "Essential CURL API Commands for Testing & Troubleshooting"
description: "Learn essential CURL API commands for testing and troubleshooting a large number of APIs critical to connecting your systems together!"
canonical: "https://adamtheautomator.com/curl-api/"
---

# Essential CURL API Commands for Testing & Troubleshooting

> Learn essential CURL API commands for testing and troubleshooting a large number of APIs critical to connecting your systems together!

Source: https://adamtheautomator.com/curl-api/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Essential CURL API Commands for Testing & Troubleshooting](https://adamtheautomator.com/wp-content/uploads/2022/07/Essential-CURL-API-Commands-for-Testing-Troubleshooting.jpg)

# Essential CURL API Commands for Testing & Troubleshooting

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)1 August 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Curl](/tag/curl/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Retrieving Data via the cURL API Command (HTTP GET Requests)](#retrieving-data-via-the-curl-api-command-http-get-requests)
*   [Sending Data to Servers with HTTP POST Requests](#sending-data-to-servers-with-http-post-requests)
*   [Updating Resources by Sending HTTP PUT Requests](#updating-resources-by-sending-http-put-requests)
*   [Deleting Resource by Sending HTTP DELETE Requests](#deleting-resource-by-sending-http-delete-requests)
*   [Securing API with RESTful Authentication](#securing-api-with-restful-authentication)
*   [Conclusion](#conclusion)

Application Programming Interfaces (APIs ) are just one of the essential pieces of any microservice or container-based architecture. And if you’re looking for a way for different services to communicate with each other, cURL API commands can do wonders for you.

In this tutorial, you’ll learn the basics of cURL API and some practical examples of how to use it for testing and troubleshooting.

Read on and get started sending requests securely to REST APIs!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

*   A Linux machine – This tutorial uses [Ubuntu](https://adamtheautomator.com/install-ubuntu/) 20.04 LTS, but the commands should work on any flavor of Linux.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   [cURL](https://www.cyberciti.biz/faq/how-to-install-curl-command-on-a-ubuntu-linux/) installed on your Linux machine.

Related:[Curl vs. PowerShell: Comparing Use Cases](https://adamtheautomator.com/powershell-curl/)

*   A REST API to test – This tutorial uses a [{JSON} Placeholder](https://jsonplaceholder.typicode.com/), a fake online REST API. This fake API allows you to test different cURL commands without messing up a real API.

Related:[Wrangling REST APIs with PowerShell JSON Examples (4 Demos!)](https://adamtheautomator.com/powershell-json/)

*   A [GitHub account](https://github.com/signup?source=login).

## Retrieving Data via the cURL API Command (HTTP GET Requests)

API testing is essential to ensure your API works as expected. One of the most common types of API tests is an HTTP GET request. You use an HTTP GET request to retrieve specific data from a server.

To make a GET request, you need to specify the URL of the server you want to connect to. GET is the default request type, so you don’t need to specify GET explicitly.

1\. Run the [curl](https://linuxize.com/post/curl-command-examples/) command below to make a GET request, and return all posts in JSON format.

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

You’ll get a long list of JSON objects in the output, including the userId, title, etc., as shown below.

![Making a GET request (”userId”: 1)](https://adamtheautomator.com/wp-content/uploads/2022/07/image-444.png)

Making a GET request (”userId”: 1)

Notice the output contains tons of posts (the list goes on), and just by looking at it, you already know that scrolling through the list feels tedious.

If you only want to retrieve a specific post or userId, jump to the following step.

![Making a GET request (”userId”: 2 and ”userId”: 3)](https://adamtheautomator.com/wp-content/uploads/2022/07/image-445.png)

Making a GET request (”userId”: 2 and ”userId”: 3)

2\. Next, run the same command below as you did in step one. But this time, append the ?userId=1 query parameter.

Query parameters tell the curl command to extract data you specified to retrieve from a server. In this case, the curl command retrieves each post with a specific userId of 1.

```bash
curl https://jsonplaceholder.typicode.com/posts?userId=1
```

![Retrieving data with a specific userId property value](https://adamtheautomator.com/wp-content/uploads/2022/07/image-446.png)

Retrieving data with a specific userId property value

3\. Lastly, rerun the same command below, and add the -i option to include the response headers in the output.

```bash
curl -i https://jsonplaceholder.typicode.com/posts?userId=1
```

You can see in the output that the status code is 200 in the response headers, which indicates the request was successful.

> _Depending on the HTTP request methods (GET, POST, PATCH, etc.) you use, the status code will be different. Read on, and you’ll learn more about each status code._

![Including the response headers in the GET request](https://adamtheautomator.com/wp-content/uploads/2022/07/image-447.png)

Including the response headers in the GET request

## **Sending Data to Servers with HTTP POST Requests**

You’ve requested data from a server by sending GET requests, but can you do the opposite and send data instead? Yes! By sending POST requests, you create new data on a server.

But it’s worth being cautious when you send POST requests. Why? Even if the data you sent via a POST request exists on the server, the POST request still overwrites the data.

Run the below command to specify (`-X`) a `POST` request to create a new post with the following data (`-d`), in key-value pairs separated by the `&` symbol:

*   A `userId` of `10`.
*   A title of `Hello World`.
*   A body of `This is the body`.

```bash
curl -X POST -i -d "userId=10&title=Hello World&body=This is the body." <https://jsonplaceholder.typicode.com/posts>
```

This time, you will get a **201** status code in the output, as shown below. In REST APIs, the 201 status code indicates the request has been fulfilled and that a new resource/data has been created.

![Making a POST request](https://adamtheautomator.com/wp-content/uploads/2022/07/image-448.png)

Making a POST request

## Updating Resources by Sending HTTP PUT Requests

So far, you have learned to retrieve and add data to a server by sending GET and POST requests. But what if you plan to update an existing resource on the server instead? In that case, sending PUT requests come into play.

Run the below command to specify (-X) a PUT request to update the data (-d) with a post ID of 1 for the userId=1.

If the resource/data to update doesn’t exist on the server, the PUT request creates a new resource with the data you provided instead.

```bash
curl -X PUT -i -d "userId=1&title=Hello World&body=This is the body." <https://jsonplaceholder.typicode.com/posts/1>
```

Below, you get a **200** status code, which indicates the request was successful. The server understood the `PUT` request and updated the data.

![Making a PUT request to update data](https://adamtheautomator.com/wp-content/uploads/2022/07/image-449.png)

Making a PUT request to update data

Now, try to run the below command to try and update the same data, but this time, without specifying the post ID number.

```bash
curl -X PUT -i -d "userId=5&title=Hello World&body=Post body." [<https://jsonplaceholder.typicode.com>](<https://jsonplaceholder.typicode.com/>)
```

The **404** status code in the output below indicates the resource you’re trying to update doesn’t exist on the specified server. With this example, you’ll be mindful of what’s causing the 404 status code.

You get this status code since you didn’t specify the post ID in the `curl` command. But an incorrect URL can also result in getting this status code.

![Sending HTTP PUT Requests For Testing](https://adamtheautomator.com/wp-content/uploads/2022/07/image-450.png)

Sending HTTP PUT Requests For Testing

## Deleting Resource by Sending HTTP DELETE Requests

Why would you want to delete a resource from the server? There can be many reasons. For instance, if you no longer use a particular resource, you might want to delete that resource from the server to save some space.

Run the command below to specify (-X) and send a DELETE request to delete the post with an ID of 2.

```bash
curl -i -X DELETE [<https://jsonplaceholder.typicode.com/posts/2>](<https://jsonplaceholder.typicode.com/posts/2>)
```

![Sending HTTP DELETE requests to delete data from the server](https://adamtheautomator.com/wp-content/uploads/2022/07/image-451.png)

Sending HTTP DELETE requests to delete data from the server

## Securing API with RESTful Authentication

Now that you know how to make various requests to a REST API, it’s time to learn how to use authentication with a REST API. Security is always a top priority anyway.

If you don’t authenticate with a REST API, anyone who has the URL of the API can make requests and get data from the API, which is not what you want.

There are two methods to authenticate with a REST API. You must implement any of these two methods below so that only authorized users can access the API:

*   Basic Authentication – The simplest way to authenticate with a REST API. In this method, you need to send your username and password with every request you make to the API. The downside of using Basic Authentication is that your username and password are sent with every request in plain text — too risky.
*   Token-Based Authentication – This method is more secure to authenticate with a REST API. In this method, you must first authenticate with the API and get a token. The token is then sent with every request you make to the API. In contrast with Basic Authentication, the benefit of using Token-Based Authentication is that your username and password are not sent with every request — more secure.

You’ll touch on Basic Authentication methods using the GitHub API so you can get an idea of how they work.

Run the below command specifying your GitHub username (-u) for Basic Authentication to make a GET request to the GitHub API. Replace your\_username with your actual GitHub username.

```bash
curl -i -u your_username [<https://api.github.com/users/octocat>](<https://api.github.com/users/octocat>)
```

After running the command, enter your GitHub password when prompted.

You’ll get a list of the GitHub repositories for the user `octocat` via the GitHub API, as shown below.

![Making a secure request to the GitHub API](https://adamtheautomator.com/wp-content/uploads/2022/07/image-452.png)

Making a secure request to the GitHub API

## Conclusion

In this article, you’ve learned essential cURL API commands for testing to work with REST APIs. You touched on sending various cURL API requests to see them in action and understand how they work.

REST APIs are everywhere nowadays. They are used by almost all the major companies, such as Google, Facebook, Amazon, and Twitter. So if you wish to work with them, this tutorial is your first step.

You’re now equipped with the basics of REST APIs, so it’s only fitting to pat yourself on the back. Now, to better understand what actually happens when you make requests to REST API, why not explore more about [various types of status codes](https://restfulapi.net/http-status-codes/)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fcurl-api%2F&text=Essential%20CURL%20API%20Commands%20for%20Testing%20%26%20Troubleshooting)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fcurl-api%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fcurl-api%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/CURL-Linux-Command-Learning-By-Example.jpg)

### [CURL Linux Command : Learning By Example](/curl-linux/)

Learn through step-by-step examples with the indispensable CURL Linux tool and level-up your troubleshooting and scripting!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Discover-the-Many-Ways-to-Get-an-IP-Address-on-Linux.jpg)

### [Get an IP Address on Linux : Discover the Many Ways](/get-an-ip-address-on-linux/)

Discover the many ways and tools that are available to get an IP address in Linux and learn how to incorporate those tools into scripts!

![](https://adamtheautomator.com/wp-content/uploads/2019/12/website-4396924_1920.jpg)

### [PowerShell vs. Curl: A Side-by-Side Comparison](/powershell-curl/)

Compare PowerShell and curl to discover the best tool for various contexts. Enhance your IT toolkit and optimize your workflow.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
