Generate dynamic content using Postman

Reference

(1) Postman provides a "pre-request scripts" functionality where a custom javascript is executed before processing the actual request. It is useful for debugging or generate dynamic content to be injected into the request.

(2) Following sample shows a script to generate SHA256 hash of several request parameters and set it as a postman variable:

var method = pm.request.method;
var path = pm.request.url.getPath();
if (pm.request.url.getQueryString()){
    path = pm.request.url.getPath() + "?" + pm.request.url.getQueryString();
}
var body = "";
if (pm.request.body) {
    body = pm.request.body;
}
var str = method + "." + path + "." + body;

var sha256digest = CryptoJS.SHA256(str.toLowerCase()).toString(CryptoJS.enc.Hex);

pm.environment.set("sha256digest", sha256digest); 

(3) This script can be set per request or the entire collection:


(4) If you set a variable via the script, you can use it in the request:


(5) You can also view the current value by mouse-over:



AI Summary
Chrome On-device AI 2024-10-04 21:50:24

Share Article