Postman: How to set authorization type for specific environment?

1k Views Asked by At

In Postman I have the following configuration:

I have Basic Auth permission for my collection. I have an Admin User environment. In this environment I have username and password. When I run my requests, the Admin user logs in and the request is sent.

I want to create an Anonymous user environment and try to access my requests without authorization (No Auth).

How do I specify an authorization type for a specific environment?

What I would like to do is in the "Pre-Request Scripts" check if the environment is equal to anonymous user and if so, do not login (using No Auth) !

More generally, I would like to test that my requests return an HTTP 401 status if I am not connected.

Any help is welcome !

1

There are 1 best solutions below

1
On BEST ANSWER

You cannot set it for a specific environment but can change it from pre reqeust script

pm.request.auth = undefined

in postman you can change all request related details using pm.request object . The authroization scheme is in the pm.request.auth

so in your case you can do something like this in your pre request script:

pm.request.auth = pm.environment.get("user")==="Anonymous"? undefined : pm.request.auth

this will set auth to undefined if use is Anonymous else the default one