Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute a md5 hash in a pre-request script in PostMan?

I have to set a parameter in my request that is a md5 hash of two other parameters. I think a pre-request script can do the job, but I do not know how to compute a md5 in this script. Any idea?

like image 257
Quentin Avatar asked Mar 11 '15 16:03

Quentin


People also ask

How do you use MD5 in Postman?

You can use Crypto JS's MD5 function, within postman directly CryptoJS. MD5(message). toString(); . Also, you can check the Postman collection on Crypto JS for more information.


1 Answers

You can create the following pre-request script provided your parameters are defined environment variables. You would need to tweak this example if they are defined in some other fashion.

// Access your env variables like this var str_1 = environment.variable_1 + environment.variable_2;  // Or get your request parameters var str_2 = request.data["foo"] + request.data["bar"];  // Use the CryptoJS var hash = CryptoJS.MD5(str_1 + str_2).toString();  // Set the new environment variable postman.setEnvironmentVariable('hash', hash); 

CryptoJS works because it's available in Postman (as well as lodash, backbone etc).

Accessing environment variables is easy through the environment object.

Setting environment variables is available through the postman object.

After this pre-request has run you can access the hash variable using the normal {{hash}} shorthand.

Also, you can read here about libraries, variables and properties available in Postman.

like image 107
darryn.ten Avatar answered Sep 19 '22 00:09

darryn.ten