Skip to main content

API Calling

Before we learn how to call an API, let's first create a new collection in Postman.

Creating a Collection

A collection is a group of related requests. It is a way to organize your requests and keep them organized. To create a new collection, first, make sure you selected any workspace.

Steps to Select a Workspace:

img.png

  1. Click on the Workspaces tab on the left sidebar.
  2. Click on the + New button to create a new workspace.
  3. Enter a name for the workspace and click on the Create button. img_1.png
  4. Select the workspace you just created. img_2.png

Now, let's create a new collection in the workspace.

  1. Click on the Collections tab on the left sidebar.
  2. Click on the + New button to create a new collection. img_3.png
  3. Enter a name for the collection. img_5.png

Create a Request

Created a collection? Great 🎉

Now, let's create a request in the collection to call an API.

Before going further, let's understand what a request is.

What is a Request?

A request is a message sent from the client to the server. It is used to request data from the server or perform an action on the server. The server then sends a response to the client.

Some requests are used to retrieve data from the server, and some requests are used to send data to the server. There are several types of requests, also known as HTTP methods.

What is an HTTP Method?

An HTTP method is a type of request used to perform a specific action on the server. There are several HTTP methods, including GET, POST, PUT, PATCH, and DELETE. There are several other HTTP methods, but these are the most commonly used methods.

Let's learn how to send a request using different HTTP methods. We'll use the JSONPlaceholder API to demonstrate how to send a request using different HTTP methods.

We'll use the same collection we created in the previous section. If you haven't created a collection, please follow the steps in the previous section to create a collection.

Let's start with the GET request.

GET Request

A GET request is used to retrieve data from the server. I will use the JSONPlaceholder API to demonstrate how to send a GET request.

  1. Click on the Collections tab on the left sidebar.
  2. There are many ways to create a request, like img_6.png
  3. Click on the Add Request button to create a new request.
  4. By default, the request method is GET, name New Request, and empty URL. img_7.png
  5. I am going to name the request Get All Posts, put the URL https://jsonplaceholder.typicode.com/posts, and select the request method GET, and click on the Save button. img_8.png
  6. Now, click on the Send button to send the request, and you will see the response in the Body tab. img_9.png
Shortcut to Send a Request

You can also send a request by pressing Ctrl + Enter.

POST Request

A POST request is used to create a new resource. I will again use the JSONPlaceholder API to demonstrate how to send a POST request.

  1. On the left sidebar, click on the Collections tab. Right-click on the First Collection.
  2. Click on the Add Request button to create a new request.
  3. Name the request Create a Post, put the URL https://jsonplaceholder.typicode.com/posts, and select the request method POST.
  4. Click on the Body tab, select raw, and JSON from the dropdown.
  5. Enter the following JSON data in the body.
    {
    "title": "foo",
    "body": "bar",
    "userId": 1
    }
  6. Click on the Send button to send the request, and you will see the response in the Body tab.
  7. You will see the response status code 201 Created and the response body with the newly created post.
  8. You can also see the response headers in the Headers tab.

PATCH Request

A PATCH request is used to update a resource. Will use the PATCH method to update the post we created in the previous section.

  1. Again, right-click on the First Collection on the left sidebar.
  2. Click on the Add Request button to create a new request.
  3. Name the request Update a Post, put the URL https://jsonplaceholder.typicode.com/posts/1, and select the request method PATCH.
  4. Click on the Body tab, select raw, and JSON from the dropdown.
  5. Enter the following JSON data in the body.
    {
    "title": "foo",
    "body": "bar",
    "userId": 1
    }
  6. Click on the Send button to send the request, and you will see the response in the Body tab.
  7. You will see the response status code 200 OK and the response body with the updated post.
  8. You can also see the response headers in the Headers tab.
  9. You can also see the updated post in the Get All Posts request.

PUT Request

PUT requests are similar to PATCH requests. The only difference is that PUT requests replace the existing resource with the new resource, whereas PATCH requests update the existing resource with the new resource.

  1. Right-click on the First Collection on the left sidebar.
  2. Click on the Add Request button to create a new request.
  3. Name the request Replace a Post, put the URL https://jsonplaceholder.typicode.com/posts/1, and select the request method PUT.
  4. Click on the Body tab, select raw, and JSON from the dropdown.
  5. Enter the following JSON data in the body.
    {
    "title": "foo",
    "body": "bar",
    "userId": 1
    }
  6. Click on the Send button to send the request, and you will see the response in the Body tab.
  7. You will see the response status code 200 OK and the response body with the replaced post.

DELETE Request

DELETE requests are used to delete a resource.

  1. Right-click on the First Collection on the left sidebar.
  2. Click on the Add Request button to create a new request.
  3. Name the request Delete a Post, put the URL https://jsonplaceholder.typicode.com/posts/1, and select the request method DELETE.
  4. Click on the Send button to send the request, and you will see the response in the Body tab.
  5. You will see the response status code 200 OK and the response body with an empty object.
  6. You can also see the response headers in the Headers tab.
  7. You can also see the deleted post in the Get All Posts request.
  8. You can also see the deleted post in the Get a Post request.

What is coming next?

In the next article, we will learn how to use Postman to test APIs. But here we have only a few requests. What if we have hundreds of requests? How do we organize them? We will learn how to organize requests in the next article.

Conclusion

In this article, we learned how to create a collection and create requests in the collection. We also learned how to send requests using different HTTP methods.