0
0
Postmantesting~15 mins

REST API fundamentals review in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify GET and POST requests on REST API
Preconditions (2)
Step 1: Send a GET request to https://jsonplaceholder.typicode.com/posts/1
Step 2: Verify the response status code is 200
Step 3: Verify the response body contains userId, id, title, and body fields
Step 4: Send a POST request to https://jsonplaceholder.typicode.com/posts with JSON body {"title": "foo", "body": "bar", "userId": 1}
Step 5: Verify the response status code is 201
Step 6: Verify the response body contains the sent title, body, userId and a new id field
✅ Expected Result: GET request returns status 200 with correct fields; POST request returns status 201 with correct fields including new id
Automation Requirements - Postman test scripts
Assertions Needed:
Response status code is 200 for GET
Response body contains keys userId, id, title, body for GET
Response status code is 201 for POST
Response body contains keys title, body, userId, id for POST
Best Practices:
Use pm.response.to.have.status for status code assertions
Use pm.expect with JSON parsing for body content assertions
Separate tests clearly for GET and POST requests
Use descriptive test names
Avoid hardcoding values except for test data
Automated Solution
Postman
pm.test('GET /posts/1 returns status 200', () => {
    pm.response.to.have.status(200);
});

pm.test('GET /posts/1 response has required fields', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('userId');
    pm.expect(jsonData).to.have.property('id');
    pm.expect(jsonData).to.have.property('title');
    pm.expect(jsonData).to.have.property('body');
});

// For POST request, create a new request in Postman with method POST and URL https://jsonplaceholder.typicode.com/posts
// Set body to raw JSON:
// {"title": "foo", "body": "bar", "userId": 1}

pm.test('POST /posts returns status 201', () => {
    pm.response.to.have.status(201);
});

pm.test('POST /posts response has sent fields and new id', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('title', 'foo');
    pm.expect(jsonData).to.have.property('body', 'bar');
    pm.expect(jsonData).to.have.property('userId', 1);
    pm.expect(jsonData).to.have.property('id');
});

This Postman test script verifies the REST API fundamentals by checking GET and POST requests.

First, it checks that the GET request to /posts/1 returns status 200 and the expected fields in the JSON response.

Then, for the POST request, it verifies the status code 201 and that the response body contains the same data sent plus a new id field.

Using pm.test and pm.expect ensures clear, readable assertions. Parsing the response JSON allows checking specific fields.

This approach helps beginners understand how to automate REST API tests in Postman with clear, simple assertions.

Common Mistakes - 4 Pitfalls
Not checking the response status code before validating the body
Hardcoding expected values without verifying the API contract
Using incorrect assertion syntax like pm.response.status instead of pm.response.to.have.status
Mixing GET and POST tests in one test block
Bonus Challenge

Now add data-driven testing with 3 different POST request bodies to verify the API handles multiple inputs correctly

Show Hint