[PostMan] Execute HTTP request in Pre-execute script

For web API integration test, some of them might need to execute some tasks as parameter for target API. In this case, pre-request script is helpful to collect data and make some data massage before test.

In this case, it use Postman as testing tools to call another web API to get token and store in environment variable.

Steps as below.

  1. Open API edit dialog.
    In Postman, select API and select Edit.
  2. Edit Pre-request Script.
    Select Pre-request Script and input script. In this demo, it will call another HTTP request to get token so append code as below.
    const requestBody={
         "username": pm.environment.get("spoc-user-name"),
         "password": pm.environment.get("spoc-user-password")
      }
    const sendRequest = {
      url: pm.collectionVariables.get("baseUrl")+"/user-management/accesses",
      method: "POST",
      header: "Content-Type:application/json",
      body: JSON.stringify(requestBody)
    };
    
    pm.sendRequest(sendRequest, (error, response) => {
        const tokens=response.json();
        pm.collectionVariables.set("spoc-access-token",tokens.authenticationResult.accessToken);
        pm.collectionVariables.set("spoc-id-token",tokens.authenticationResult.idToken);
    });
  3. Save settings.
    Click Update to save and apply settings.
About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.