DEV Community

Cover image for Introduction to Postman Scripts
Dilpreet Johal
Dilpreet Johal

Posted on

Introduction to Postman Scripts

In this post, we will do a quick introduction to Postman Scripts. Learning scripting will give you the power to automate your API collections, so it’s really important you understand how Scripting works if you want to do API automation using Postman.

What are Postman Scripts?

Postman Scripting allows you to add dynamic behavior to requests and collections by executing JavaScript code. Essentially, what you are doing is using JavaScript code to manipulate your API requests.

There are 2 types of scripting you can do with Postman –

  • Pre-requests scripts
  • Test scripts

Pre-request Scripts – manipulate the data of the request before Postman sends the request

Test Scripts – validate the response of the request by writing tests (gets executed after Postman receives the response)

What can you do with the Scripts?

Postman Scripts allow you to change the behavior of the requests, let’s understand what that means. Using Scripts you can do some of the following:

  • Set or Get the variable values / parameters / headers / body data etc… in the request directly
  • Debugging (log output to the console)

Let’s take a look at an example –
Postman Scripts

Let’s assume you are creating a user using the POST request and then you need to fetch the id of that user in your GET request. Instead of manually copy/pasting the id, you can take advantage of scripts to do that for you.

First, you can set a variable called ‘id’ in the Tests script of the POST request. Then, get the variable in the Pre-request script of the GET request and set it in the parameter.

This allows us to automate this flow using Postman collection without having to worry about relying on existing data.


Check out the video below to learn more about Postman Scripts – –


To learn about API Test Automation using JavaScript, check out my free tutorial series here –

JavaScript API Test Automation Tutorial Series


📧 Subscribe to my mailing list to get access to more content like this
👍 Follow @automationbro on Twitter for the latest updates

...

I love coffees! And, if this post helped you out and you would like to support my work, you can do that by clicking on the button below and buying me a cup of coffee -

Buy me a coffee

You can also support me by liking and sharing this content.

Thanks for reading!

Top comments (0)