Tools Tutorial menu

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:

PartExample
MethodGET, POST, PUT, PATCH, DELETE
URLhttps://api.example.com/products/5
HeadersContent-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

  1. Click New, then HTTP Request.
  2. Keep the method as GET.
  3. Enter the URL https://jsonplaceholder.typicode.com/users/1 (a free practice API).
  4. Click Send.

Postman shows the response:

Output
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

  1. Change the method to POST and the URL to https://jsonplaceholder.typicode.com/posts.
  2. Open the Body tab, choose raw, and select JSON.
  3. Enter the body and click Send:

Example

json
{
  "title": "Admissions open",
  "body": "New batch starts next Monday.",
  "userId": 1
}
Output
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

CodeMeaning
200 OKSuccess
201 CreatedA new item was created
204 No ContentSuccess, nothing to return
400 Bad RequestThe data you sent is wrong
401 UnauthorizedYou are not logged in or the token is missing
403 ForbiddenLogged in, but not allowed
404 Not FoundThe address or item does not exist
500 Internal Server ErrorThe 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

text
Authorization: Bearer YOUR_TOKEN_HERE

Collections 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 to http://localhost:3000 while 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

bash
curl https://jsonplaceholder.typicode.com/users/1

Example

bash
curl -X POST https://jsonplaceholder.typicode.com/posts -H "Content-Type: application/json" -d "{\"title\":\"Hello\",\"userId\":1}"
Output
{
  "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?

Learn Tools with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp