Open In App

How to execute post-request scripts in Postman?

Last Updated : 07 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. It can make various types of HTTP requests(GET, POST, PUT, PATCH), save environments for later use, and convert the API to code for various languages(like JavaScript, and Python). In this article, we will learn How do you execute post-request scripts in Postman.

Prerequisites:

Steps to Execute Post-Request Scripts in Postman:

Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like “GFG”. Here, we can see, multiple tabs like Authorization, Pre-request scripts, Tests , Variables.

Step 2: Click on Tests. Now in the right side pane, we can multiple snippets arrived. We can use any of the available snippet or create our own code based on our requirement. Code is written in Javascript.

z165

Step 3: Click on Save

Step 4: When you hover, on the name of your collection, 3 dots will appear. Click on those 3 dots, and then click on “Add new request”

z159

Step 5: Now You can simply paste the API in the space provided and select the API type you are requesting from the dropdown like GET,POST, PUT, DELETE etc. Output will be shown in the body with the status code.

Step 6: You can see the Post-execution scripts results in tab Test Results; as shown below

z166

Example: Lets create a collection “GFG”. We have written 2 tests/post-requests scripts.

First Test: This will check whether the response time is less then 200ms

pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

Second Test: This will check whether status code is 200

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

Output: We are a creating a GET request to this API; https://jsonplaceholder.typicode.com/posts/1

Recording-2023-12-03-at-101007


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads