3

How to call a POST API request (Login API having request body with Username & Password field) from pre-request script tab of an another GET API which uses token from above API's body in its request url.

Login API : POST method; request body : username and password; response body: token. Get Customer Records API : GET method; request URI : /token/

Want to cover this end to end scenario in one test only in Postman. Can please anyone help me with the pre-request script for this? How should I invoke Login API?

Amrit
  • 89
  • 1
  • 4
  • 11

2 Answers2

7

I just had the same issue and found the solution here.

In a gist, you can pass in a request object instead of the URL to the request.

const loginRequest = {
    url: 'http://example.com/login',
    method: 'POST',
    header: 'Content-Type: application/json',
    body: {
        mode: 'application/json',
        raw: JSON.stringify({        
            "username": pm.environment.get("username"),
            "password": pm.environment.get("password")
        })
    }
};


pm.sendRequest(loginRequest, function (err, response) {
    pm.environment.set("accessToken", response.json().token);
});

That's all there is to it.

UPDATE I just found the detailed info in the Postman docs.

Barnabas Kecskes
  • 1,861
  • 17
  • 24
  • Is there some solution to call existing request, without manualy creating? – Andrew Sneck Sep 27 '19 at 15:58
  • The link in your update is now broken @barnabas-kecskes – Matt W Feb 06 '20 at 08:54
  • @barnabas-kecskes Are you sure this code should work in pre-request? I've copied it pretty much directly and cannot get it to work. The current postman docs say "Pre-request scripts are written in JavaScript, and the syntax is similar to the test scripts except that the response object is not present." – Matt W Feb 06 '20 at 16:35
  • Hey @MattW thanks for the heads up - I have updated the documentation link for you. It looks there was no change in how you send the request itself? – Barnabas Kecskes Feb 07 '20 at 10:56
  • Yes, I am sending the request the same way you are and in the docs you link to. The problem is that those docs are for the Tests scripting functionality and not the Pre-request Script functionality of Postman. The docs, as I say above, explicitly rule out being able to retrieve the response body - and my testing bears this out. – Matt W Feb 07 '20 at 14:43
  • Hmmm interesting @MattW, I just had a more thorough look on this and you are absolutely right: they explicitly state that there is no body in the response. I have the feeling that this wasn't the case earlier in previous versions... Cookies and headers are accessible though. ‍♂️ – Barnabas Kecskes Feb 08 '20 at 15:27
0

It's possible for sure.

  1. On "Pre-request Script" tab in Postman, make a POST request using the jQuery ajax method to your login API.
  2. Save the response body(your token) into the environment variable(postman.setEnvironmentVariable()).
  3. Set your /token/ part of your Postman request url as /{{your_variable_name}}/.

Have a look at this.

viz
  • 1,247
  • 16
  • 27
  • Yeah, even I think that what I am trying to achieve can't be supported in Postman (Pre-request script). I know the concept of Collection runner, it was just that I am looking for some workaround to use Pre-request script tab to achieve the goal. Please kindly see this link & let me know if this can work in our case : https://medium.com/@MrPonath/postman-pre-request-scripts-81920a78d0fc ~ @viz – Amrit May 26 '17 at 05:12
  • @Amrit Oh I haven't noticed that Postman has a pre-request script feature. Try the edited answer. – viz May 26 '17 at 05:35
  • 1
    Ok will try it & let you know. I'm little new to API testing so don't know whether its going to work or not. Use of Ajax calls in Pre-request tab is what the article says. – Amrit May 26 '17 at 05:39
  • I fixed some wordings to make it clear. Have a try and let me know. – viz May 26 '17 at 05:47