SyntaxStudy
Sign Up
REST API Testing REST APIs with Postman
REST API Beginner 1 min read

Testing REST APIs with Postman

Postman is the most popular tool for manually exploring and testing REST APIs. Collections group related requests, environments hold variables like base URLs and tokens, and the pre-request script / test script hooks let you automate token refresh, chain requests, and write assertions using the Chai assertion library. Postman Collections can be exported as JSON and committed to version control, turning them into living documentation. The Newman CLI runs collections in CI/CD pipelines, reporting failures as JUnit XML that test frameworks like Jenkins and GitHub Actions can parse.
Example
// Postman: Pre-request Script — auto-refresh expired JWT
const tokenExpiry = pm.environment.get('tokenExpiry');
const now = Math.floor(Date.now() / 1000);

if (!tokenExpiry || now >= tokenExpiry - 60) {
    pm.sendRequest({
        url: pm.environment.get('baseUrl') + '/auth/refresh',
        method: 'POST',
        header: { 'Content-Type': 'application/json' },
        body: {
            mode: 'raw',
            raw: JSON.stringify({ refreshToken: pm.environment.get('refreshToken') })
        }
    }, (err, response) => {
        const json = response.json();
        pm.environment.set('accessToken', json.accessToken);
        pm.environment.set('tokenExpiry', now + json.expiresIn);
    });
}

// Postman: Test Script — assert response shape
pm.test('Status is 200', () => pm.response.to.have.status(200));
pm.test('Has data array', () => {
    const json = pm.response.json();
    pm.expect(json).to.have.property('data').that.is.an('array');
    pm.expect(json.data.length).to.be.greaterThan(0);
});
pm.test('First product has required fields', () => {
    const product = pm.response.json().data[0];
    pm.expect(product).to.have.all.keys('id', 'name', 'price', 'status');
    pm.expect(product.price).to.be.a('number').and.above(0);
});
pm.test('Response time < 500ms', () => pm.expect(pm.response.responseTime).to.be.below(500));