How to Use pm.sendRequest in Postman for API Testing
Use
pm.sendRequest in Postman scripts to send HTTP requests programmatically during test execution. It takes a request object and a callback function to handle the response, enabling chaining or conditional API calls within your tests.Syntax
The pm.sendRequest function requires two main parts: a request configuration object and a callback function. The request object defines the HTTP method, URL, headers, and body. The callback function receives the response to perform assertions or further actions.
javascript
pm.sendRequest({
url: 'https://api.example.com/data',
method: 'GET',
header: {
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify({ key: 'value' })
}
}, function (err, res) {
// handle response here
});Example
This example sends a GET request to a public API and checks if the response status is 200. It demonstrates how to use pm.sendRequest inside a test script to make a secondary API call and validate its response.
javascript
pm.sendRequest({
url: 'https://jsonplaceholder.typicode.com/posts/1',
method: 'GET'
}, function (err, res) {
pm.test('Response status is 200', function () {
pm.expect(err).to.be.null;
pm.expect(res).to.have.property('code', 200);
});
pm.test('Response has userId', function () {
const jsonData = res.json();
pm.expect(jsonData).to.have.property('userId');
});
});Output
PASS Response status is 200
PASS Response has userId
Common Pitfalls
- Not handling the asynchronous nature of
pm.sendRequestcan cause tests to finish before the request completes. - Forgetting to check for errors in the callback leads to false positives.
- Incorrect request object structure, such as missing
methodor malformedbody, causes request failures.
Always include error checks and use pm.expect inside the callback to ensure proper test validation.
javascript
/* Wrong way: Missing error check and assertions outside callback */ pm.sendRequest({ url: 'https://jsonplaceholder.typicode.com/posts/1', method: 'GET' }); pm.test('Check status', () => { pm.expect(pm.response.code).to.eql(200); // This will fail because pm.response is not from sendRequest }); /* Right way: Use assertions inside callback and check errors */ pm.sendRequest({ url: 'https://jsonplaceholder.typicode.com/posts/1', method: 'GET' }, (err, res) => { pm.test('Status is 200', () => { pm.expect(err).to.be.null; pm.expect(res).to.have.property('code', 200); }); });
Quick Reference
Here is a quick summary of key points when using pm.sendRequest:
| Feature | Description |
|---|---|
| Request Object | Defines URL, method, headers, and body for the HTTP request. |
| Callback Function | Receives error and response to handle results and assertions. |
| Asynchronous | Runs asynchronously; assertions must be inside the callback. |
| Use Cases | Chaining requests, conditional logic, or fetching data during tests. |
| Error Handling | Always check err in the callback to catch request failures. |
Key Takeaways
Use pm.sendRequest to send HTTP requests programmatically within Postman scripts.
Always place assertions inside the callback function to handle asynchronous responses correctly.
Check for errors in the callback to avoid false test results.
Define the request object clearly with method, URL, headers, and body as needed.
Use pm.sendRequest for chaining API calls or conditional testing scenarios.