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.
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)
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 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.
📧 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 -
You can also support me by liking and sharing this content.
Thanks for reading!