1

Is it possible to call another request in a request using Postman?

I have a collection named A

Inside A I have a POST request named Login and a GET request named Profile

Profile is dependent on Login so in Profile Pre-Request Script will need to call Login

But I can't seem to find a way to call Login

From what I can gathered so far is using

pm.sendRequest({
  url: "api/Login",
  method: 'POST',
  header: { "Content-Type": "application/json"},
  body: {
    mode: 'raw',
    contentType: 'application/json',
    raw: JSON.stringify({
      username: "username",
      password: "password"
    })
  }
}

But for me it is not user friendly.

Cause that what if I change one of the body data in Login but forgot to edit it in the Pre-Request Script in Profile.

Any suggestions and help are highly apparated. Thanks

Zorev Gnoz
  • 221
  • 4
  • 20

1 Answers1

1

I would use the Postman collection runner to accomplish this.

Essentially this is what you'll want to do:

  1. Add some code in the Tests tab for your Login request that captures the token (or whatever the Profile request is dependent on), and sets it as a collection variable.

    Example:

    const { token } = pm.response.json();
    
    pm.collectionVariables.set('token', token);
    
  2. Reference the collection variable (token) in the Profile request, by double curly quotes like so: {{token}}.

  3. Run the collection. The Login request will generate the token, save it in the collection variables, and then the Profile request will reference it.