LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » Internet / Internet Information » How To Use Pre-Request Scripts & Tests In Postman Using For Environment Variables?

How To Use Pre-Request Scripts & Tests In Postman Using For Environment Variables?

How-To-Use-Pre-Request-Scripts-&-Tests-In-Postman-Using-For-Environment-Variables
In analogy to the unit testing world, the pre-request script is nothing but the setup that will happen before a test is executed. Similarly in Postman, if you want to modify the request in a certain way, the pre-request script is the place to put that logic or code that guarantees before the request execution starts.

You can use Postman Pre-Request Scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request Script tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data. You can also use pre-request scripts for debugging code, for example by logging output to the console.

Postman Tests confirm that your API is working as expected, that integrations between services are functioning reliably, and that any changes haven’t broken existing functionality. You can write test scripts for your Postman API requests in JavaScript. You can also use test code to aid the debugging process when something goes wrong with your API project. For example, you might write a test to validate your API’s error handling by sending a request with incomplete data or wrong parameters.

Use-Pre-Request-Scripts-&-Tests-In-Postman-Using-For-Environment-Variables

Table of Contents

How To Encode Data Before Sending In Request In Postman?

If looking to Base64 encode/decode.

The easiest way is to use JavaScript methods btoaatob

Look at the example in the below code.

Pre-Request Scripts:

var API_KEY = pm.environment.get('API_KEY');
var ENCODED_API_KEY = "Basic " + btoa(API_KEY + ":");
console.log(`Authorization: ${ENCODED_API_KEY}`) //if you want to see the value in the console
pm.environment.set("Authorization", ENCODED_API_KEY);

Postman-Request-And-Receive-Flow

Tests (Post-Request Scripts):

var jsonData = JSON.parse(responseBody);
pm.test("If Login Is Successfull", () => {
  pm.expect(jsonData.status).to.eql(1);
});
postman.setEnvironmentVariable("authtoken", jsonData.data.AuthToken);

Conclusion:

In this tutorial, we dwelled on pre-request scripts and tests or post-request scripts/tests. We also walked through an example of using these scripts at the collection level to avoid repetition and placing common scripts at the collection level itself.

Both pre-request scripts and tests are a very powerful and important feature of Postman and they add a lot of value in creating an end-to-end integration test, particularly for REST-based API endpoints.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *