10
votes

I want to set basic Authorization in Postman using environment variable. Because I have different authorization username and password for the different API calls.

I set my postman according to below:

In Authorization Tab: I've selected No Auth
In Header Tab: Key=Authorization Value= Basic{{MyAuthorization}}
In Body Tab:

{
    "UserName": "{{UserName}}",
    "ServiceUrl": "{{ServiceUrl}}"
}

//which set it from the envitonment variable

In Pre-request Tab:

// Require the crypto-js module
var CryptoJS = require("crypto-js");

// Parse the `username` and `password` environment variables
let credsParsed = CryptoJS.enc.Utf8.parse(`${pm.environment.get('admin')}:${pm.environment.get('admin')}`);

// Base64 encoded the parsed value
let credsEncoded = CryptoJS.enc.Base64.stringify(credsParsed);

// Set the valuse as an environment variable and use in the request
pm.environment.set('MyAuthorization', credsEncoded);
console.log(credsEncoded);

In Test Tab:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("LoginInfoID", jsonData.First.LoginInfoID);

Then I've sent the request and got unauthorized.

After that, I've set auth type to basic auth with username and password it's working fine and I got what I wanted from the response.

2
Can you show your current Postman pre-request script? How do you determine username/password for "different" API? - shaochuancs
actually for now i passed through normal key and value format. - Arbaz.in
What does your environment file look like? Did you add the variables so that they can be referenced from the script? What's in the test tab? - Danny Dainton
yes i created environment file with variables and also set in to scrip issue with when i set different type except Basic Auth i i got 403 ERROR - Arbaz.in
${pm.environment.get('admin')}:${pm.environment.get('admin')} That would get the value of admin that you saved in the environment file twice so it wouldn't be correct. - Danny Dainton

2 Answers

20
votes

Another way which worked for me:

  1. Set up environment variables for 'username' and 'password', and save
  2. In the Authorization tab of the request, select Basic Auth
  3. In the Username field, enter {{username}}
  4. For the Password field, click "Show Password", and enter {{password}}

Hope this helps others :)

3
votes

You could use cryptp-js in a Pre-request Script with a very crude solution like this:

// Require the crypto-js module
var CryptoJS = require("crypto-js");

// Parse the `username` and `password` environment variables
let credsParsed = CryptoJS.enc.Utf8.parse(`${pm.environment.get('username')}:${pm.environment.get('password')}`);

// Base64 encoded the parsed value
let credsEncoded = CryptoJS.enc.Base64.stringify(credsParsed);

// Set the valuse as an environment variable and use in the request
pm.environment.set('authCreds', credsEncoded);

You could add your credentials to a set of different environment files, under the key username and password.

In the request, just set the Header like this:

Postman

You can also set those at the Collection / Sub-folder level so you're not repeating yourself in each request.

It's one way you could achieve this but there will be other ways too.