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.