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.
- Open API edit dialog.
In Postman, select API and select Edit. - 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); });
- Save settings.
Click Update to save and apply settings.
Leave a Reply