Postman API 101
In this article, I am gonna write about APIs and how to test them using Postman.
So you must have heard of an API if you are a developer and I will be discussing it in detail.
What is an API and how does it work?
Application Programming Interface or API is a pipeline to transfer data from client application to server and vice versa in from of requests
and responses
or in other words, an API acts as a medium of communication between the client and server over a network. Imagine that you went to your favorite restaurant, sitting at the table you call the waiter to order something (say pizza) then the waiter passes your order to the chef. Now there are two possibilities either you get your pizza or the waiter returns with a message that "Sorry sir the pizza you just ordered is currently out of stock please order something else "
Here you are the client-side application
the chef
is the server and the waiter
is an API that is passing your request
to the server and brings a response
for you.
Why do we need an API?
Consider a situation where you are building an app like Uber and it will have some basic services like authentication
, maps
for location service email service
for sending invoices and newsletters or payment service
for accepting payments. Now you can access all these services through APIs like Google maps
location services auth0
for handing the authentication amazon sqs
for emails and stripe
for payments. All I want to say is that you can access any software service throughAPI's
who doesn't have to create all these services from scratch. Also using an API
will be more cost-effective than creating service from scratch. And believe me, there is an API
for everything, all you need to do is implement it.
What is Postman and why should we use it?
Postman is an API
collaborative development tool which makes it easy to create, test, develop and use an API
through a wonderful UI
. And it is used by global and top fortune organizations like PayPal
, Uber
, etc. So test any API, you can share your works or develop an API with your team at the same time using a public or private workplace. You can even write tests or check for security issues, and many more.
Request and Response in depth
As I have mentioned earlier, how an API
works now let's understand about the request
and responses
in-depth. Talking about request
when you search for something in google you are actually making a request to the Google server passing a query. See the image below, you can see the URL is https://www.google.com/search?q=hello
here https:\\
is the protocol for your request www.google.com
is the address of the server
, \search
is the endpoint
and ?q=hello
your query
.
Suppose the teacher asked Ram to bring him a book for the library. HereAsking
is the request protocol ram
is the hostname for the server, library
is the endpoint and book
is the query.
Request methods
GET -Retrieve information
POST -Send information
PUT/PATCH -Update information
DELETE -Delete information
Now if you have requested something you are definitely going to get a response with a status code. Making it simple, your search results will be your responsibility. An API can return a repose in many forms like Html
, XML
, JSON
,etc. and the most important thing that response brings is a status code. Status codes help us to understand the type of our response. Suppose if you send a GET
request you may get data with a status code of 200
means success or may get 404
means not found or if you send PUT
or POST
or DELETE
you may get 201
for created 204
no content or 500
internal server error .
Start using postman
Create a workspace (means creating a project)
Go to Home>Workspaces>Create Workspace
Name your workspace> Add a summary > select the visibility > hit on Create Workspace
Create a collection
Collection means the group of saved requests. We can create a new collection or can simply fork
(means copying) a collection to our workspace. To create a new collection in your workspace, on the left click on+
> add a name to your collection > click on :
to add a new request.
Fork a collection
visit a collection > click on ...
> scroll down and select create a fork
> add a label and select your workspace location > click on create fork
Get
A getthe
request is used when we want to get some data from an API. Suppose I ask you what is the name? you will reply to me with your name (Say, my name is Jhon). In this example you are the API, my question is the get request and your answer is the response. Now let's perform this task in real.
Open the postman click on the +
tab to create a new request and then select Get request from the dropdown and paste this URL in the Enter request URL
place holder and then click on the send.
Post
A post
the request is used when we want to send data to a server using an API. Suppose you have written something on a paper, so in this case, what you have written is the data, and the pencil you are using is the API and the paper is the server.
Now again in the postman then paste the same URL, this time choose post request from the dropdown, then click on body > raw >json and write some JSON content in the placeholder, and hit the send button.
"userId": 697,
"id": 10,
"title": "post request ",
"body": "this request is created by kk"
}
Put
A put
the request is used when we want to update any data that we have posted earlier to the server, using API. Suppose you have written an essay on a paper and now you want to update something in it, in this case, Your pencil will be API and the paper will be the server and you can update anything on the paper
Now again in the postman then paste the URL, this time choose put
to request from the dropdown, then click on body
> raw
>json
and write some JSON content in the placeholder this time let's change the content.
{
"title":"This title is updated"
}
Delete
A delete
request is used to delete some data from a server using an API. Considering the last example, after writing an essay, now you want to erase it using an eraser, in this case, your paper will be the server and the eraser will be the API. Now again in the postman let's send a delete request to this URL,