Pre-request scripting with Postman version v0.10

Last week we launched our new logo and we were really happy to see that people loved it. That was not all however. We also released new versions of Postman and Newman.

As noted in the previous post, the app icon for the packaged app was updated in line with the new Postman logo. Apart from bug fixes, the other big change though is something that a lot of users had requested – pre-request scripting.

Pre-request scripts are similar to test scripts except as the name implies, they are executed just before your request is going to be sent to the server by Postman. Some sample use cases for pre-request scripts are:

  1. Generating values and injecting them in requests through environment variables. For example, you can generate timestamps or hashes of values.
  2. Handle custom authentication protocols.
  3. Read and modify data from JSON or CSV files and inject them into scripts.

The primary flow while working with pre-requests would be setting an environment or a global variable and then using the variable in your HTTP request. Let's look at an example:

  1. Open Postman and click on the "Pre-request scripts" button.

1

  1. Set the request URL to http://echo.getpostman.com/post and set the method to POST. Keep the body type as form-data.

2

  1. Inside the pre-request editor, write the script below:

4

What we are doing here is generating a timestamp and setting an environment variable. This environment variable will then be used in the Postman request builder interface.

  1. Set the environment variable in the form-data key value editor.

5

  1. Create a new environment called "echo.getpostman.com" in the environment manager modal.

7-environment

  1. Select the environment from the selector on the top right.

8-select

  1. Hit "Send" and analyze the response.

6

As evident above, the server receives the value of the variable which was dynamically generated.

Pre-request scripts can be used in creative ways and can save you a lot of time. If you come up with an interesting use case, make sure to let us know on Twitter or email! Check out the official docs page here. We will be updating it with more examples continuously.

We have also become regular with updating changelogs for Postman and Newman. Make sure to check them out to stay up to date!