0
0
Postmantesting~15 mins

Basic authentication in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify API access with Basic Authentication
Preconditions (3)
Step 1: Open Postman and create a new GET request
Step 2: Enter the URL https://api.example.com/data in the request URL field
Step 3: Go to the Authorization tab
Step 4: Select 'Basic Auth' from the Type dropdown
Step 5: Enter username 'user1' and password 'pass123'
Step 6: Click Send to submit the request
✅ Expected Result: The API responds with status code 200 and returns the expected data in JSON format
Automation Requirements - Postman test scripts
Assertions Needed:
Verify response status code is 200
Verify response body contains expected data fields
Best Practices:
Use environment variables for username and password
Use pm.response.to.have.status for status code assertion
Use pm.expect with JSON parsing for response body validation
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response has expected data', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('data');
    pm.expect(jsonData.data).to.be.an('array');
});

The first test checks that the API response status code is 200, which means the request was successful.

The second test parses the response body as JSON and verifies it contains a property named 'data' which should be an array. This confirms the API returned the expected data structure.

Using pm.response.to.have.status and pm.expect are Postman's recommended ways to write assertions in test scripts.

Environment variables for username and password help keep credentials secure and reusable.

Common Mistakes - 3 Pitfalls
Hardcoding username and password directly in the request
Not verifying the response status code
Assuming response body is always JSON without parsing
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations

Show Hint