Practice: testing APIs with tools
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Most API testing mistakes look the same in Postman and curl: a missing header, a variable that does not resolve, a test that checks nothing, a run that leaves data behind. Each has a real run, then mixed practice.
Mistake 1: translating a request and losing a header
Every Postman request maps to one curl command, and the hidden Content-Type is the part people lose. The example shows the 415.
Example 1 · Works in Postman, 415 in curl
curl -s -X POST -d @product.json -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/v1/productscurl -s --json @product.json -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/v1/products$ curl -s -X POST -d @product.json -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/v1/products
{"type":"https://shoplite.example/problems/unsupported-media-type","title":"Unsupported media type","status":415,"detail":"Content-Type application/x-www-form-urlencoded is not supported. Send application/json.","instance":"/api/v1/products"}
$ curl -s --json @product.json -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/v1/products
{"id":16,"sku":"BK-WINGS","name":"Wings of Fire","categoryId":5,"categoryName":"Books","price":349.00,"stock":40,"isActive":true}What this code does
- The first command is a hand translation of a Postman request with Body → raw → JSON. It forgot the Content-Type that Postman adds, so
-dsent the form type and got415. --jsonaddsContent-Type: application/json(andAccept) and uses POST, so the same file creates product 16.
The method becomes -X (or comes from --json), each Headers row becomes -H, the Authorization tab becomes -u or -H "Authorization: Bearer ...", and a raw JSON body becomes --json. The Code button does this translation for you; by hand, the hidden headers are the ones people forget.
The usual slip: Postman's raw → JSON adds Content-Type: application/json silently, but the hand-written curl uses -d without a header, which sends the form type. The API answers 415, and the request "works in Postman but not in curl". Use --json or add the header.
Mistake 2: a variable that never resolves
A variable that is not defined is sent as plain text. The example saves productId but uses productID.
Example 2 · productId was saved, productID was used
Create product Post-response: pm.collectionVariables.set("productId", pm.response.json().id);Get product GET {{baseUrl}}/products/{{productID}}$ newman run shoplite-api.postman_collection.json -e local.postman_environment.json --env-var "adminPassword=$SHOPLITE_ADMIN_PASSWORD" --reporter-cli-no-summary --reporter-cli-no-banner
ShopLite API
→ Log in as admin
POST http://localhost:8080/api/v1/auth/login [200 OK, 231B, 109ms]
√ Status is 200
√ Token is returned
→ Create product
POST http://localhost:8080/api/v1/products [201 Created, 296B, 10ms]
→ Get product
GET http://localhost:8080/api/v1/products/{{productID}} [400 Bad Request, 343B, 7ms]What this code does
- The create worked and stored the id as
productId. The next URL asks forproductID, a different name with no value, so the text{{productID}}went to the server. - The API answered
400because{{productID}}is not a number. Fix the name in the URL, not the API.
A {{variable}} that is not defined in any active scope is sent as plain text. In the host it gives ENOTFOUND; in a path it reaches the server as /products/{{productID}} and the API answers 400 or 404. The cause is nearly always a typo in the name (names are case-sensitive), a variable set in another scope than you think, or no environment selected.
Check in this order: hover over the variable in the URL bar (red means unresolved), compare the name letter by letter with the script that sets it, and look at the environment dropdown.
Mistake 3: a test that cannot fail
A test passes unless something inside it throws. The example shows a green test on a 404.
Example 3 · A green test on a 404
// Weak: nothing can make it failpm.test("Product is found", function () { const product = pm.response.json(); product.name === "Wings of Fire";});// Honest: checks the status, then the fieldpm.test("Product is found", function () { pm.response.to.have.status(200); pm.expect(pm.response.json().name).to.eql("Wings of Fire");});$ newman run shoplite-api.postman_collection.json -e local.postman_environment.json --reporter-cli-no-summary --reporter-cli-no-banner
ShopLite API
→ Get product (weak test)
GET http://localhost:8080/api/v1/products/99 [404 Not Found, 291B, 61ms]
√ Product is found
→ Get product (honest test)
GET http://localhost:8080/api/v1/products/99 [404 Not Found, 291B, 6ms]
1. Product is found
# failure detail
1. AssertionError Product is found
expected response to have status code 200 but got 404
at assertion:0 in test-script
inside "Get product (honest test)"What this code does
- Product 99 does not exist, yet the weak test passed: the comparison was computed and thrown away, and nothing threw an error.
- The honest test checks the status first and fails with a clear message:
expected response to have status code 200 but got 404.
A line such as product.name === "Wings of Fire"; computes true or false and throws nothing, so the test is green even for a 404. The same happens with a test that only logs values, or one that checks the wrong thing, such as the status of the login instead of the create.
Every test needs a pm.expect or pm.response.to... line. Check the status first, then the fields. To trust a new test, break it on purpose once (expect a wrong value) and watch it fail.
Find the bug
Find the test that cannot fail
Click the line that stops the test from checking anything.
This test is green even when the product is not found.
Round 1 of 2Mistake 4: a run that leaves data behind
A run that leaves data behind passes once and then fails. The example creates the same SKU twice.
Example 4 · Green the first time, red the second
Log in as admin -> saves {{token}}Create product -> POST {{baseUrl}}/products with SKU BK-WINGS, test: status is 201(no delete request)The same collection, run twice in a row$ newman run shoplite-api.postman_collection.json -e local.postman_environment.json --env-var "adminPassword=$SHOPLITE_ADMIN_PASSWORD" --reporter-cli-no-summary --reporter-cli-no-banner
ShopLite API
→ Log in as admin
POST http://localhost:8080/api/v1/auth/login [200 OK, 231B, 86ms]
√ Status is 200
√ Token is returned
→ Create product
POST http://localhost:8080/api/v1/products [201 Created, 296B, 8ms]
√ Status is 201
$ newman run shoplite-api.postman_collection.json -e local.postman_environment.json --env-var "adminPassword=$SHOPLITE_ADMIN_PASSWORD" --reporter-cli-no-summary --reporter-cli-no-banner
ShopLite API
→ Log in as admin
POST http://localhost:8080/api/v1/auth/login [200 OK, 231B, 105ms]
√ Status is 200
√ Token is returned
→ Create product
POST http://localhost:8080/api/v1/products [409 Conflict, 308B, 8ms]
1. Status is 201
# failure detail
1. AssertionError Status is 201
expected response to have status code 201 but got 409
at assertion:0 in test-script
inside "Create product"What this code does
- The first run created BK-WINGS and passed. Nothing deleted it, so the second run's create hit the unique SKU rule and got
409 Conflict. - Add a Delete product request (with
{{productId}}saved from the create) and a check that it returns 204; then both runs pass.
A collection that creates a product with a fixed SKU and never deletes it passes once. The second run gets 409 Conflict, because that SKU now exists. Teams then say the tests are "flaky": sometimes green, sometimes red, for no clear reason.
A repeatable run cleans up after itself: create, check, delete, and check the delete. When cleanup is not possible, use values that are unique per run. Run the collection twice in a row before you trust it.
Finish up
Mark complete & continue
Next: Mini project: a tested Postman collection for a public API