postman sendRequest use authorization from collection
Asked Answered
K

2

8

I can send a request in a Postman pre-request script as seen below. What I would like to do is use the same authentication as is set in the Collection so that if the Collection changes then my sendRequest follows suite.

pm.sendRequest({
    url: 'http://some_url',
    method: 'GET',
    header: {
        'content-type': 'application/json',
        'authorization': 'Basic ' + auth
    },

}, function (err, res) {
    // do something
});

The reason I want to do this is that I want to share the collection with partners and customers and each of them may use a different authentication type. At the moment I have configured my collection to use basic authentication and have used variables for user name and password. If, for example, a partner switches the collection to use OAuth then they will need to also update all of my pre-request scripts to use OAuth.

If pm.sendRequest() was able to inherit the authentication just as each request in the collection can then the partner could make the change in one place.

Kunstlied answered 22/2, 2019 at 3:34 Comment(4)
what do you mean by set in the Collection?Collete
What type of Auth is it and how are you using this in the collection? You could save it as a variable and use the pm.variables.get('my_token_var') syntax in the pre-request script. Need more context and information on the question though.Fidler
I have updated the post to provide more context.Kunstlied
Docs don't make it clear that .sendRequest() doesn't take on the collection context as "Send" does. Needing to manually specify headers again this way implies requests should work different than those in the collection, but not in script. I think this is a mistake. Put another way, it seems odd to be required to script in auth headers in pre-request AND in tests.Behoove
P
8

This will work, assuming you're executing sendRequest after a request which had an authorization header as well:

pm.sendRequest({
    url: 'http://some_url',
    method: 'GET',
    header: {
        'content-type': 'application/json',
        'authorization': request.headers["authorization"]
    },

}, function (err, res) {
    // do something
});

Information on the Request object can be found here.

Paleoecology answered 26/2, 2019 at 23:2 Comment(1)
Yes that is a good idea. The way I am doing it ATM is to set a variable with the information I am interested in, in the Tests in 'request a'. Then in 'request b' I check for that variable in the pre-request script and throw an Error telling the user to run 'request a' before 'request b'.Kunstlied
B
2

UPDATE:

I don't know if it's too late, but it worked for me by creating the request like this:

const host = pm.environment.get("host");
const path = "/oauth/token";
        
pm.sendRequest({
  url: host + path,
  method: 'POST',
  auth: {
    type: "basic",
    basic: {
      username: 'BASIC_USER',
      password: 'BASIC_PASS'
    }     
  },
  headers: {
    Accept: "*/*"
  },
  body: {
    mode: 'urlencoded',
    urlencoded: [
      { key: "grant_type", value: "password" },
      { key: "username", value: "VALUE1" },
      { key: "password", value: "VALUE2" }
    ]
  }
}, function (err, res) {
  if (err === null) {
    pm.environment.set("token", res.json()["access_token"]);
  }
});

I hope it can be useful to someone.

Belindabelisarius answered 17/10, 2023 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.