What is API

API is an acronym for Application Programming Interface. For example, we want to get data from the webserver. Now the application will send the request to the server and get the data back and present it to us in a human-readable format. This is what API does.

Creating API Request

The first step of creating a new API request is to create a new workspace so that all the request can be distinguished from each other. To create a new workspace, start the POSTMAN software and click on the My Workspace icon.

postman-workspace-icon

When you click on the My Workspace Button, then you will see a create new option popping out. Click on that, and name a new workspace.

postman-create-new-workspace

The workspace name can be any name. In my case, I have named it with workspace01. And then, click the Create Workspace button at the bottom.

postman-create-new-workspace-name

Click the New button on the top left corner of the POSTMAN homepage interface, and once you click the button, a drop-down menu appears that contains all the shortcuts.

Select the request option from the drop-down list.

postman-select-new-request

Now, as you click on the Request option, you will be prompted to enter a request name.

postman-save-request

We will be using the website, http://reqres.in for testing the API.

Now, as we scroll down the page (http://reqres.in), you will see that several Requests are listed there. We shall select the Single User. So, click the Single User and then copy the URL from the search bar. And copy the request endpoint.

postman-api-request

Now open a new tab in the browser type the main URL along with the Request endpoint. Once you press enter after giving the complete link(http://reqres.in/api/users/2) into the URL bar. You must see the response from the server.

postman-response

After that, add a name to the Request and create a new collection and save the request and the collection as well.

postman-request-and-collection

In my case, the request name is the Get Response, and the collection name is Collection01.

Now, copy the request URL(http://reqres.in/api/users/2) and paste it into the URL bar in the POSTMAN.

postman-paste-url

Once done, now simply click the send button.

postman-send-button

Then, you must see the response as below:

postman-response-from-server

So far we have seen how we create our first API and get the response. Now that are different methods such as POST, GET, PUT, UPDATE, etc which can be used to update, retrieve or insert data into the server. In our next section, we shall see how different methods can be used in POSTMAN.

Methods in POSTMAN

There are different methods in which are available in POSTMAN that can be used while sending the request to get the response or to change the data on the server. In the POSTMAN, there is a drop-down button beside the request URL bar where you can select the type of method you want to use. The different types of methods are:

  • GET
  • POST
  • PATCH
  • OPTIONS
  • PUT
  • UPDATE
  • DELETE
GET Method

The GET method is used to access the data from the server. First Select the GET method from the drop-down button beside the URL bar and then copy the API request address:

Visit the link: http://reqres.in

And then select a GET method Request:

postman-get-request-option

Copy the main URL and then copy the request end-point and then paste it into the URL bar and set the method type as GET.

Enter the request URL as:

https://reqres.in/api/users?id=4

postman-get-users

As you can see, I require the details of the person whose id is 4. Therefore, I have added my query as: ?id=4 in the request end-point.

Now, click on the Send button and see what is the response. You will see the details of only one person whose id is 4.

Response :

{
    "data": {
        "id": 4,
        "email": "eve.holt@reqres.in",
        "first_name": "Eve",
        "last_name": "Holt",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
    }
}
POST Method

The POST method is there to create new data into the server. First Select the POST method from the drop-down button beside the URL bar and then copy the API request address:

Visit the link: http://reqres.in

And then select a POST method Request:

postman-post-request-selection

And then after selecting the method type as POST and pasting the Request URL, go into the body section and click into the raw and edit field you want. I have edited the field- name as I want to create a new people called Rahul.

postman-method-type-post

Now, click the Send button and see the response.

The response is:

{
    "name": "Rahul",
    "job": "leader",
    "id": "221",
    "createdAt": "2019-10-21T10:42:44.974Z"
}

So, you can see that the name is now Rahul.

PUT

The PUT method is mainly used to update attributes of existing details in the server. First Select the PUT method from the drop-down button beside the URL bar and then copy the API request address:

Visit the link: http://reqres.in

And then select a PUT method Request:

postman-put-method-type

Now, go to the POSTMAN software, and change the method type as PUT and then paste the Request URL along with the request end-point.

Once, you click the Send button, the request will be sent and response will be generated. Now, copy a part of the response you want to update and paste it into the raw code editor under the body section.

postman-copy-response

I have already copied the response, so directly paste below code into the raw code section:

{
            "id": 2,
            "email": "janet.weaver@reqres.in",
            "first_name": "Riya",
            "last_name": "Weaver",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
 }

Now, paste the same code into the body section under raw code:

postman-copy-raw-section

Now, change the value of any attribute in the raw code section and send the request again. I shall change the value of the attribute first_name. I shall change it from Riya to Alexa.

postman-put-method-example

Now, click the Send button and check the response. The response, I got, is below:

{
    "id": 2,
    "email": "janet.weaver@reqres.in",
    "first_name": "Alexa",
    "last_name": "Weaver",
    "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg",
    "updatedAt": "2019-10-22T03:02:34.831Z"
}

You can see that in the response, the first_name value has been updated to Alexa.

PATCH

In the case of the PUT method, when any field or attribute information has to be updated then the complete record for that attribute has to be updated. Even if you have to update a single field, then also the entire record has to be pasted into the raw section and then the request has to be sent.

But in the case of the PATCH method, you can just mention the parameters which directly address the record can be used to update the record.

For example, in my case, I will be using the attribute id for identifying my individual records.

First, I check the record by appending the id (anyone you want, I used id =2)with the request URL.

Edit the request URL as:

https://reqres.in/api/users?id=2

Click the Send button, and send the request using the GET method to see the response.

postman-check-get-value

Now, copy change the method type into PATCH and then change the value of the email, by editing in the body-raw section.

postman-patch-example

Now, click the Send button to send the request and see the response from the server. See below image, that the response of the server says, that the email has been updated.

postman-patch-updated

DELETE

Any record from the database can be deleted using the DELETE method in POSTMAN. To delete a record, we need an attribute that uniquely identifies the record. In my case, it is the id.

Edit the Request URL as:

https://reqres.in/api/users?id=2

postman-delete-method

Now, click on the Send button to execute the Request. As the response must be like below:

postman-delete-response

In the above image, you can see that there are no values returned from the server. Now, the status code is 204 No Content. It means, that the request is executed successfully, but no data record with id=2 has been received from the server. It means the record with id-2 has been deleted from the server.

OPTIONS

Options method is for indicating which methods can be applied to a requests. In a particular request, we may be able to apply the GET method but not the POST method.

Let us see with an example. We shall use the same Request URL as we are using, and then select the method type as Option. Then click the Send button, and see the header section of the response.

postman-options-method-example

In the above image, you can see that the method type is: OPTIONS. And so in the response, under the Header section, the Access-Control-Allow-Method is: GET, HEAD, PUT, PATCH, POST, DELETE. It means these methods are suggested by POSTMAN and can be applied to the given Request URL.

HEADER Method will not retrieve any body from the server. This method is basically used to retrieve the header content from the server, which contains the meta-data information, content length and the data and time of the response.

The HEAD method can be used to ping the server to check whether a request is working fine. Also, it can be used to validate the content type and the request.

To use the HEAD method, just change the method type into HEAD and then click into the Send button to send the request.

postman-head-method-example

In the above image, you can see that the content-type is shown, along with the date, etc.

Generating Code Snippets of Requests

With the help of Code Snippets, requests can be translated into codes and can be executed in the forms of codes. In the below example we shall see how to generate codes corresponding to a request.

Enter the request URL into the Request Bar and then click the code link into(on the right side of the POSTMAN interface).

postman-generate-code

Once you click on the code link, you will see a different option for languages into which you want to translate your request code. Some of the options are:

  • JSON
  • PHP
  • C#
  • JSON
  • JAVA
  • Javascript
  • etc.

Once you click into the code link, you must see the below window as follows:

postman-code-language-option

I have selected my language of choice as cURL, so it's like:

postman-code-generated

Editable code:

curl -X GET \
  https://reqres.in/api/users \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Cookie: __cfduid=d9197045e87789f8024fb186385a5f1891570648581' \
  -H 'Host: reqres.in' \
  -H 'Postman-Token: aa9dfa22-3fd7-49e2-99c9-7d7cd277004d,57c7f45b-28f7-4ee8-a8dd-55dff1ede11f' \
  -H 'User-Agent: PostmanRuntime/7.18.0' \
  -H 'cache-control: no-cache'

Now copy the code into the cmd or terminal of your system and hit an enter to run the Request.

postman-copy-paste-code-cmd

After hitting enter you will get the response from the server into the command prompt itself.

{"page":1,"per_page":6,"total":12,"total_pages":2,"data":[{"id":1,"email":"george.bluth@reqres.in","first_name":"George","last_name":"Bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"},{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"},{"id":3,"email":"emma.wong@reqres.in","first_name":"Emma","last_name":"Wong","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg"},{"id":4,"email":"eve.holt@reqres.in","first_name":"Eve","last_name":"Holt","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"},{"id":5,"email":"charles.morris@reqres.in","first_name":"Charles","last_name":"Morris","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"},{"id":6,"email":"tracey.ramos@reqres.in","first_name":"Tracey","last_name":"Ramos","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"}]}
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions