Postman is a powerfull graphical HTTP client helpful when working with APIs. I’ll introduce a few tricks to get started with it!
Installation
To install Postman, just go to this link and download the right version for the OS you use.
How to do a simple GET?
Now, you should see the following screen:
Click the Request button at the top-left of the modal, and you’ll get on the Save Request screen.
Give it a name, create a collection if you don’t have any, select a collection and save it.
From there, you’ll be able to send a request. For exemple, you can hit the HTTPCat
API. Just enter the https://http.cat/[status_code]
URL in the dedicated tab, with the status code of your choice. Press Send and you’ll see the response just below.
A first POST with a JSON body
To do a POST
request, we are going to hit the https://jsonplaceholder.typicode.com/
URL, on the post
route.
So from the same screen, select POST
instead of GET
, and enter the URL in the dedicated tab.
The API we’re hitting enables us to send any JSON body, and sends it back in the response, adding it an id
. Let’s then send a random object.
Go in the Body tab, check the raw radio button, and make sure that you selected JSON (application/json) on the dropdown on the right.
Now let’s write our JSON object. There are a couple things we need to be careful of when we write a JSON object, especially when you are used to write JavaScript. Check here to know the traps not to fall in.
Which gives us in the end this kind of POST requests:
Just press Send and you’ll have the response!
Useful tricks POSTMAN offers
How to share one’s collections
Export them
On the left of your window, you have a list of your collections. Go over one with your mouse cursor, and you’ll see three dots on the bottom-right of it. Click them, then click Export.
You’ll get on a modal that asks you to choose the way you want to download your collection. Choose Collection v2, click Export and save it.
Import them
To import a collection, simply click on Import at the top-right of your window, then get in your own files the json you want to import!
How to set environnement variables
The tipical use case here is when you work with different environments. Let’s say you develop an API which has a production, a staging and a development environments. For each of them, you want to set a Host
and a token
variable, to be able to auhtenticate.
Click the wheel, then select Manage Environments.
Click Add.
You can now give a name to your environment, and set the variables you need. Click Update when this is done.
You can do this for each one of your environments, then you’ll see them on the Manage Environments.
Now go back to the main screen, and fill your request with the variables using double curly brackets: {{ }}
.
Best way to create a request: copy as curl
Let’s say you want to reproduce a request done on a website. But this request is a very tricky one with, for example, authentication, complicated headers, a big body or a method you don’t really know how to use.
Then the best way to replicate this request is to basically copy the entire request directly from your browser.
Let’s say we want to get the post request of a research on https://giphy.com/. Open the browser development tools (Right click + Inspect on Chrome). Go in the Network tab and filter the requests by POST
methods. Right click the one you are interested in, and select Copy as CURL.
Now, go back to POSTMAN. On the top-left, click Import, and go in the Paste Raw Text tab. Simply paste what you copied, and press Import.
Now simply Send the request and you’ll have the answer!
Conculsion
If you want to keep going with Postman, you check this article about Postman Cloud, which explains you how to share and document your API.
Hum, actually I think I only counted 4 tricks.. Well, anyway, if you find a fifth one just send it to me and I’ll add it!
The post 5 Tricks to Master Postman appeared first on Theodo.