Encoding variables using Postman

Here is how I handle key/secret encoding for connecting to appigee ouath

let keys = pm.environment.get("key") + ":" + pm.environment.get("secret");
let encodedKeys = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(keys));

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

Using just stringify as proposed by the currently accepted answer I got an odd error stating r.clamp is not a function. To fix that I had to parse the keys first.


According to Change request body in pre-request script you cannot change request body in postman unless you use variables.

If looking to Base64 encode/decode.

Easiest way is use JavaScript methods btoa, atob

Here are the steps taking below JSON as your request

{
  "UserName": "[email protected]",
  "Password": "{{base64EncodedPassword}}",
  "ConfirmPassword": "{{base64EncodedPassword}}",
  "Role": "SuperAdmin"
}
  1. Define first environment variable in your postman environment called password (assuming you looking to encrypt password only). Make sure you set your unencrypted password value.
  2. Define second environment variable called base64EncodedPassword. Leave its value blank.
  3. You can then use following code in Postman Pre-request script tab.
pm.environment.set("base64EncodedPassword", btoa(pm.environment.get("password")));

It's possible using Postman Pre-request Scripts and Postman Environment Variables .

First step is to set up the variables you want to encode.

Next, write a script. Postman has CryptoJs built-in, which you can use to encode strings. Here's a sample script to Base64 encode a string and set an environment variable to the encoded value:

var plainText = pm.environment.get('plainTextString');
var encoded = CryptoJS.enc.Base64.stringify(plainText);

console.log(`Encoded value: ${encoded}`) //if you want to see the value in the console
pm.environment.set("myEncodedRequestVariable", encoded);

Finally, use your encoded variables in the request body (or header) with this syntax:

{{myEncodedRequestVariable}}