17

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.

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
Arbaz.in
  • 1,478
  • 2
  • 19
  • 41
  • Can you show your current Postman pre-request script? How do you determine username/password for "different" API? – shaochuancs Jul 04 '19 at 09:05
  • actually for now i passed through normal key and value format. – Arbaz.in Jul 04 '19 at 09:11
  • 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 Jul 04 '19 at 15:30
  • 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 Jul 05 '19 at 04:59
  • Can you show what you have in the different tabs - You're currently only showing part of what you can see so it's difficult to _know_ what's going on. What does the request look like in the Postman Console (Bottom left > Third Icon)? This will show what's being sent. In the first image you have failing tests but in the second image is shows no tests were run - Are these the same requests? Is there a header missing - One say 11 and the other says 12. How about creating the request again in a different tab? – Danny Dainton Jul 05 '19 at 08:42
  • @DannyDainton i updated my question with currant postman request – Arbaz.in Jul 05 '19 at 09:14
  • You're getting the `admin` variable twice. This should be getting `username` and `password` keys and not the value that you want to use in the fields. Make sure there is a space between `Basic` and the token value. – Danny Dainton Jul 05 '19 at 10:11
  • @DannyDainton it's username and password and i put space between basic and auth – Arbaz.in Jul 05 '19 at 10:17
  • 1
    `${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 Jul 05 '19 at 10:18

2 Answers2

48

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 :)

bjones01001101
  • 1,071
  • 1
  • 12
  • 21
5

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.

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • which type should i select from Authorization tab in postman? – Arbaz.in Jul 04 '19 at 09:52
  • You don't need to select any of them, just add the header to the request. The `basic` option in the Auth tab is just going to do the same thing as the script is doing but the script does this in a more easily changeable way. – Danny Dainton Jul 04 '19 at 10:32
  • but when set No Auth or any other of them instead of Basic auth got unauthorized error got this ERROR **JSONError | No data, empty input at 1:1** – Arbaz.in Jul 04 '19 at 11:12
  • Can you update the original question with the details of what you're seeing in Postman and the way you have it set up. Its difficult to see what's going on when you're describing something in the comments. – Danny Dainton Jul 04 '19 at 11:41