Postman and API Testing
API testing tools let you send requests to an API and see exactly what it answers, without building a website or app first.
What is an API Request?
When an app talks to a server, it sends an HTTP request with:
| Part | Example |
|---|---|
| Method | GET, POST, PUT, PATCH, DELETE |
| URL | https://api.example.com/products/5 |
| Headers | Content-Type: application/json, Authorization: Bearer abc123 |
| Body (for POST/PUT) | {"name": "Mouse", "price": 900} |
The server returns a status code, headers and usually a JSON body.
Postman
Postman is the most popular API testing app. Download the free desktop app, or use it in the browser. Similar free tools are Insomnia, Bruno and the Thunder Client extension for VS Code.
Your First GET Request
- Click New, then HTTP Request.
- Keep the method as GET.
- Enter the URL
https://jsonplaceholder.typicode.com/users/1(a free practice API). - Click Send.
Postman shows the response:
Status: 200 OK Time: 182 ms Size: 1.2 KB
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
...
}Sending a POST Request
- Change the method to POST and the URL to
https://jsonplaceholder.typicode.com/posts. - Open the Body tab, choose raw, and select JSON.
- Enter the body and click Send:
Example
{
"title": "Admissions open",
"body": "New batch starts next Monday.",
"userId": 1
}Status: 201 Created
{
"title": "Admissions open",
"body": "New batch starts next Monday.",
"userId": 1,
"id": 101
}This practice API pretends to save the post and returns it with a new id.
Status Codes to Know
| Code | Meaning |
|---|---|
| 200 OK | Success |
| 201 Created | A new item was created |
| 204 No Content | Success, nothing to return |
| 400 Bad Request | The data you sent is wrong |
| 401 Unauthorized | You are not logged in or the token is missing |
| 403 Forbidden | Logged in, but not allowed |
| 404 Not Found | The address or item does not exist |
| 500 Internal Server Error | The server's code crashed |
A 4xx code means the problem is in the request (your side). A 5xx code means the problem is on the server.
Headers and Authorization
Many APIs need a key or token. In Postman, open the Authorization tab, choose Bearer Token and paste it, or add a header yourself in the Headers tab:
Example
Authorization: Bearer YOUR_TOKEN_HERECollections and Environments
- A collection is a folder of saved requests, for example all requests of your shop API. You can share it with your team.
- An environment stores variables like
{{baseUrl}}. Set it tohttp://localhost:3000while developing and to your live URL later, and every request switches at once.
Testing from the Terminal with curl
curl is a command-line tool included in Windows 10+, macOS and Linux:
Example
curl https://jsonplaceholder.typicode.com/users/1Example
curl -X POST https://jsonplaceholder.typicode.com/posts -H "Content-Type: application/json" -d "{\"title\":\"Hello\",\"userId\":1}"{
"title": "Hello",
"userId": 1,
"id": 101
}Add -i to also see the status code and headers. curl is handy on servers, where there is no Postman.
Exercise
Exercise
You send a POST request and get status 401. What is the most likely problem?
