0
0
Postmantesting~15 mins

JSON body in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify POST request with JSON body to create a new user
Preconditions (3)
Step 1: Open Postman and create a new POST request
Step 2: Set the request URL to https://api.example.com/users
Step 3: In the Body tab, select raw and choose JSON format
Step 4: Enter the following JSON body: {"name": "John Doe", "email": "john.doe@example.com", "age": 30}
Step 5: Send the request
Step 6: Observe the response status code and body
✅ Expected Result: Response status code is 201 Created and response body contains the created user details with name 'John Doe', email 'john.doe@example.com', and age 30
Automation Requirements - Postman/Newman
Assertions Needed:
Verify response status code is 201
Verify response JSON body contains 'name' with value 'John Doe'
Verify response JSON body contains 'email' with value 'john.doe@example.com'
Verify response JSON body contains 'age' with value 30
Best Practices:
Use environment variables for endpoint URLs
Use JSON schema validation for response body
Use descriptive test names
Use pre-request scripts if needed for setup
Avoid hardcoding sensitive data
Automated Solution
Postman
pm.test('Status code is 201', function () {
    pm.response.to.have.status(201);
});

pm.test('Response has correct user details', function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.name).to.eql('John Doe');
    pm.expect(jsonData.email).to.eql('john.doe@example.com');
    pm.expect(jsonData.age).to.eql(30);
});

The first test checks that the response status code is 201, which means the user was created successfully.

The second test parses the JSON response body and verifies that the 'name', 'email', and 'age' fields match the expected values sent in the request.

This ensures the API correctly processed the JSON body and returned the expected data.

Common Mistakes - 4 Pitfalls
Not setting Content-Type header to application/json
{'mistake': 'Using incorrect JSON syntax in the body', 'why_bad': 'Invalid JSON causes the request to fail or the server to return an error.', 'correct_approach': "Validate JSON syntax before sending; use Postman's raw JSON editor."}
Hardcoding URLs and data without using variables
Not verifying response status code
Bonus Challenge

Now add data-driven testing with 3 different user JSON bodies to create multiple users

Show Hint