REST API fundamentals review in Postman - Build an Automation Script
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.
Now add data-driven testing with 3 different POST request bodies to verify the API handles multiple inputs correctly
Practice
Solution
Step 1: Understand HTTP methods
GET is used to request data without modifying it, while POST, PUT, DELETE change data.Step 2: Identify method for data retrieval
Since we want to retrieve data without changes, GET is the correct method.Final Answer:
GET -> Option BQuick Check:
Retrieve data = GET [OK]
- Confusing POST with GET for data retrieval
- Using DELETE to get data
- Thinking PUT retrieves data
Solution
Step 1: Locate HTTP method selector in Postman
Postman has a dropdown menu next to the URL field to choose HTTP methods.Step 2: Confirm correct method selection
Selecting POST from this dropdown sets the request method properly.Final Answer:
Select POST from the dropdown next to the URL input -> Option CQuick Check:
Method dropdown sets HTTP method [OK]
- Typing method name in URL field
- Trying to set method in Headers
- Confusing Body tab with method setting
Solution
Step 1: Understand DELETE success responses
A successful DELETE often returns 204 No Content, meaning the resource was deleted and no content is returned.Step 2: Compare other status codes
200 OK means success with content, 201 Created is for new resource creation, 404 Not Found means resource missing.Final Answer:
204 No Content -> Option DQuick Check:
DELETE success = 204 No Content [OK]
- Choosing 201 Created for DELETE
- Assuming 200 OK always returns content
- Confusing 404 with success
Solution
Step 1: Understand 405 error meaning
405 Method Not Allowed means the server recognizes the URL but the HTTP method used is not supported on that endpoint.Step 2: Eliminate other options
Incorrect URL usually gives 404, empty body usually does not cause 405, server down causes no response or 5xx errors.Final Answer:
The API endpoint does not support POST method -> Option AQuick Check:
405 = Method not allowed on endpoint [OK]
- Confusing 405 with 404 Not Found
- Assuming empty body causes 405
- Thinking server down causes 405
Solution
Step 1: Identify method for updating data
PUT is the standard HTTP method to update existing data in REST APIs.Step 2: Choose Postman feature to send data
The Body tab allows sending JSON data payload, which is needed to update user data.Step 3: Evaluate other options
GET does not update data, DELETE removes data, and URL query parameters are not suitable for sending JSON update data.Final Answer:
PUT method with Body tab to send JSON data -> Option AQuick Check:
Update data = PUT + Body JSON [OK]
- Using GET to update data
- Sending JSON in URL parameters
- Confusing DELETE with update
Ready to prove it?
Pass the test and this pattern moves unknown
