How to set basic authorization from environment variable in postman?
Asked Answered
T

2

21

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.

Troublous answered 4/7, 2019 at 8:51 Comment(9)
Can you show your current Postman pre-request script? How do you determine username/password for "different" API?Eberle
actually for now i passed through normal key and value format.Troublous
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?Pence
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 ERRORTroublous
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?Pence
@DannyDainton i updated my question with currant postman requestTroublous
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.Pence
@DannyDainton it's username and password and i put space between basic and authTroublous
${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.Pence
X
51

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

Ximenez answered 21/1, 2020 at 17:9 Comment(5)
this is the answer 💯Marlenamarlene
This is by far the answer that most directly addresses the questionAlkene
Yeah thanks. It's not immediately obvious that you can do this because Postman doesn't colour the Password field orange when you add the placeholder like in other places. Tried this and it works.Cari
Thank you! Something this simple should really be listed on their UI.Eton
Make sure your current values are set. I only had initial values and was always getting an empty string for username/password.Denture
P
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.

Pence answered 4/7, 2019 at 9:39 Comment(4)
which type should i select from Authorization tab in postman?Troublous
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.Pence
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:1Troublous
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.Pence

© 2022 - 2024 — McMap. All rights reserved.