Test API Requests with Postman
November 3, 2020 • APIs, Testing • Published By Josh • 4 minute read
Prior to coding, it’s helpful to test your API requests and review the responses. There are several tools out there, but we’ll be focusing on how to use Postman to test API requests!
Table of Contents
Installation
You have two options:
- Download and install the Postman application.
- Sign up for a free account and use its online version.
GET Request
Using the PokeAPI, let’s set up a simple GET request for data about Pikachu. To quickly open a new request screen, select the plus icon.
Set the request type to GET and request url to https://pokeapi.co/api/v2/pokemon/pikachu.
Select the Send button. You’ll see the response appear below like the following:
The body of the response can be displayed in different formats, but I prefer the Pretty format as its easiest to read. The response section also displays the response status code (200), how long it took to get a response (134 ms), and the size of the response body (187.44 KB).
POST Request
Certain types of requests require data to be sent with the request. A post request is a common example that usually requires data to be sent.
Set your request type to POST and define your request url. In the Body tab, select the type of body you’ll be sending. A common api request body is usually in the form of JSON. Select raw and then JSON as the type. Define your JSON data and send the request!
Authorization
There are many scenarios where APIs will require authorization. If no authorization is provided, the API request will not work. Authorization usually involves including a token in your request. This can be done in the Headers tab of your request by adding a key called Authorization and a value with your token.
Collections
Recreating requests every time you open Postman would be time consuming. You can save your requests and organize them using collections! Select the Save button after creating your request.
At the Request name field, you can keep it as the request url or provide another name. At the Request description field, you can provide some optional text about the request. Markup is also allowed. Under the All Collections section, you can create a new collection or select an existing one you previously created.
Collections can be found under the Collections tab. Selecting a collection will show/hide all saved requests under the collection. Selecting a request will open that request.
There’s a helpful option for exporting collections, which can be used to share collections with other team members. The option can be found by hovering over the collection and selecting View more actions.
Environments
An api request url in a development environment is probably different than what is used in a production environment. Instead of saving duplicate requests where the only difference may be the host, you can create environments with variables specific to each environment! Select the Manage Environments button.
Select Add to create a new environment. Define a name for your environment and then define variables and values specific to your environment. For example:
- A host variable for the host portion of the api request
- A token variable for the authorization token being sent as part of the api request
To use a specific environment, select it from the Environments list. You can include variables by surrounding your variable name with double braces like the following:
Global Variables
If you need variables that aren’t specific to an environment, you can use global variables. Select the Manage Environments button and then select the Globals button.
Maybe your user id is the same across any environment. You could create a global variable called me for example. Using a global variable is the same as using an environment variable. Surround your variable name with double braces: {{me}}.