note
This article was last updated on November 8, 2023, 1 year ago. The content may be out of date.
Postman is an API platform for developers. For average users, it can be used to send HTTP requests very easily. It has an intuitive interface - you can specify different aspects of an HTTP request: the method, the url, the query parameters, the headers and the body if any.
Sometimes it’s also easier to call an API using Postman than to use the UI to do something. Postman has some features to make this accessible.
note
It’s assumed you have a Postman account and has logged in. All the requests are done in the workspace.
Group Relevant Requests Using Collections
Collections are like folders - you put several requests there with common features there. It also supports sub folders if you want to further categorize your requests.
In this image, I put NameSilo related requests in the namesilo
collection, and OpenSubtitles requests in opensubtitles
collection. I also specified a name for each request, so I know which request does what.
Automatically Add Authorization
Some API calls need authorization to use it. Some of them are a special header sent with the request, others are special query parameters. Both of these two types of authorization can be done on a per-request basis or to a whole collection.
As we can see, the API key can be added to the header or the query parameters.
info
I find it easier to add the API key in the Pre-request Script
instead, because some APIs require several fixed query parameters in addition to the API key.
To add query parameters before sending a request, we use pm.request.addQueryParams(['key1=value1', 'key2=value2', 'key3=value3'])
. The documentation isn’t clear about this, but it can be inferred from the SDK.
Templates Rendering
Sometimes we just want some fields from an API response. Using templates we can cut out the clutter and extract the information we want. To do this, we need to write scripts under the Tests
tab on the request we want to visualize by rendering a template.
Below is how I list my NameSilo dns records:
const result = xml2Json(pm.response.text());
const template = `
<table bgcolor="#FFFFFF">
<tr>
<th>id</th>
<th>type</th>
<th>host</th>
<th>value</th>
</tr>
{{#each response}}
<tr>
<td>{{record_id}}</td>
<td>{{type}}</td>
<td>{{host}}</td>
<td>{{value}}</td>
</tr>
{{/each}}
</table>
`;
pm.visualizer.set(template, {
response: result.namesilo.reply.resource_record
})