To send a request via the sandbox, you can use pm.sendRequest.
pm.test("Status code is 200", function () {
pm.sendRequest('https://postman-echo.com/get', function (err, res) {
pm.expect(err).to.not.be.ok;
pm.expect(res).to.have.property('code', 200);
pm.expect(res).to.have.property('status', 'OK');
});
});
Without additional options, this will sent a GET request to the URL specified. If you prefer to be more explicit, you can use the complete syntax:
pm.sendRequest({
url: 'https://postman-echo.com/post',
method: 'POST',
header: 'headername1:value1',
body: {
mode: 'raw',
raw: JSON.stringify({ key: "this is json" })
}
}, function (err, res) {
console.log(res);
});
Pre-req script example of how to perform POST request before performing GET request
Example task:
I have POST request that returns response like "/chargesresponse/some-uid-numbers", and I have GET request which I want to parse mentioned POST request's answer to it's URL. So I need to make POST-request, then add it's response to GET-request URL.
https://mydomain-for-getting-charges-list.com/my-sub-path/chargesresponse/some-uid-numbers?exampleParameter1=true&exampleParameter2=true
Example solution achieved via variable in URL and pre-req script:
GET https://mydomain-for-getting-charges-list.com/my-sub-path{{myVariable123}}?exampleParameter1=true&exampleParameter2=true
Pre-request script:
Tests script example of how to parse field value from POST response body
We have some POST request that returns response with many fields, e.g.:
These fields have string values between the quotes.
And we have GET request like "https://someurl.com/value of field1"
The example task is to automate parsing value of field1 from POST's response to GET's url.
To do so we need to write such Test in POST's request:
As you see, jsonData.field1 is the most important part of the script.
Thus remains only write the variable into the url of GET request: https://someurl.com/{{field1_for_get}}
Now you can perform POST request, then perform GET request, and GET request will have parsed field1 value automatically, so you will perform 2 steps with just a 3 clicks of a mouse.