DEV Community

Mudacumura Brunoblaise
Mudacumura Brunoblaise

Posted on

2 neat Postman tricks you should know.

Introduction
We use postman every day and if not, you should consider using it, postman is one of the most powerful tools that developers use to test their Apis but most of them don't use its full potential. In this article we are going to go over 2 tricks that could save your time.

Prerequisites

This is all beginner friendly.

Let's get started! 🀞

We will walk ourselves from easy to hard.

1. Generating code

Postman can help you integrate API calls into your applications by generating code snippets in various coding languages.

let's see an example

Every request you make in postman, has a generation tab where you can generate code.

Image description

Once you open code generation tab you can choose language of your choice

Image description

2. Introduction to variables and scripting

Postman allows you to save values as variables so that you can:

Reuse values to keep your work DRY (Don’t Repeat Yourself)
Hide sensitive values like API keys from being shared publicly

image description

The GIF below shows how to create a variable locally in your collection.

variable creation

Now how do we set in programmatically?

Postman allows you to add automation and dynamic behaviors to your collections with scripting.

Postman will automatically execute any provided scripts during two events in the request flow:

Immediately before a request is sent: pre-request script (Pre-request Script tab of request).
Immediately after a response comes back: test script (Tests tab of request).

The pm object

Postman has a helper object named pm that gives you access to data about your Postman environment, requests, responses, variables and testing utilities.

let's see an example

Image description

For example, you can access the JSON response body from an API with:

pm.response.json()
Enter fullscreen mode Exit fullscreen mode

You can also programmatically get collection variables like the value of baseUrl with:

pm.collectionVariables.get(β€œbaseUrl”)
Enter fullscreen mode Exit fullscreen mode

In addition to getting variables, you can also set them with

pm.collectionVariables.set("variableName", "variableValue")
Enter fullscreen mode Exit fullscreen mode

like this:

pm.collectionVariables.set(β€œmyVar”, β€œfoo”)
Enter fullscreen mode Exit fullscreen mode

alt text

and that is all I know I should have gone deep but my goal wasn't to go deep, and I have provided links for someone who wants to go advanced in testing

Credits

  • Massive credit goes to @postmanacademy for the high-quality images, information and the inspiration to make this article.

Thanks 😎 for reading if you find this interesting leave a follow and make sure to Follow me on GitHub @brunoblaise❀

Top comments (0)