Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Test a REST API with Postman: Step-by-Step

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Testing a REST API is essential to verify that endpoints return the correct data, handle errors properly, and respond within acceptable time limits. Postman is a popular tool that lets you send HTTP requests, inspect responses, and automate tests without writing code. This guide walks you through the entire process—from setting up your first request to running automated test scripts—so you can validate your API confidently.

What You'll Need

  • Postman installed (desktop app or web version at postman.com)
  • A REST API endpoint to test (public example: https://jsonplaceholder.typicode.com/users)
  • Basic understanding of HTTP methods (GET, POST, PUT, DELETE)
  • Optional: API documentation or a spec (OpenAPI/Swagger) for reference

Step-by-Step

  1. Launch Postman and create a new request. Click the New button (or the + tab) and select HTTP Request. A new tab opens with a URL bar and method dropdown.
  2. Choose the HTTP method. From the dropdown next to the URL field, select the method your endpoint expects. For example, GET to retrieve data, POST to create a resource.
  3. Enter the request URL. Paste the endpoint URL into the address bar. For a first test, use https://jsonplaceholder.typicode.com/users with GET.
  4. Add headers (if needed). Click the Headers tab. Common headers include Content-Type: application/json for POST/PUT, or Authorization: Bearer <token> for authenticated endpoints. Postman auto-adds some headers, but you can override them.
  5. Add a request body (for POST/PUT/PATCH). Click the Body tab, select raw, and choose JSON from the format dropdown. Enter a valid JSON payload, for example:
    {
      "name": "John Doe",
      "email": "john@example.com"
    }
  6. Send the request. Click the blue Send button. Postman will display the response status, time, size, and body below.
  7. Inspect the response. Look at the Status code (e.g., 200 OK, 404 Not Found), the Time taken, and the response body. For JSON responses, Postman pretty-prints the data; you can switch between Pretty, Raw, and Preview views.
  8. Save your request. Click Save, give it a name, and create or choose a collection. This organizes your tests and allows reuse.
  9. Write a basic test script. In the request tab, click the Tests tab. Enter a simple JavaScript test using Postman's pm API:
    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });
    Click Send again. The test results appear in the Test Results tab under the response area.
  10. Add more assertions. For example, check the response time or validate a field:
    pm.test("Response time is less than 500ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(500);
    });
    
    pm.test("User has an id", function () {
        const jsonData = pm.response.json();
        pm.expect(jsonData[0]).to.have.property('id');
    });
  11. Run all tests in a collection. Click the collection name on the left, then the Run button (play icon). Configure the run (e.g., choose the environment, iterations) and click Run Collection. Postman executes each request in order and shows a summary of pass/fail.
  12. Use environment variables (optional). Create an environment (gear icon → Manage Environments) with variables like baseUrl . In the URL field, use . This makes it easy to switch between dev, staging, and production.

Common Mistakes

  • Forgetting to set Content-Type. When sending JSON, if you don't set Content-Type: application/json, the server may not parse the body correctly.
  • Using the wrong HTTP method. Sending a POST to a GET-only endpoint will likely return 405 Method Not Allowed.
  • Not checking the response body. A 200 status doesn't always mean the data is correct—always verify the payload matches expectations.
  • Hardcoding URLs. If you change environments, hardcoded URLs break. Use variables.
  • Ignoring error responses. Test not only happy paths but also 400, 401, 404, and 500 responses to ensure your API handles errors gracefully.

Tips

  • Use the Collection Runner to run tests in sequence and reuse a data file (CSV/JSON) for data-driven testing.
  • Write tests that check the schema or key fields, not just the status code.
  • Use the Pre-request Script tab to set up variables (e.g., generate a timestamp) before the request runs.
  • Leverage Postman's Console (bottom left) to debug network calls and see request/response details.
  • Export your collection and share it with your team, or use Postman's documentation generator to create API docs.

Troubleshooting

  • Request hangs or times out. Check your internet connection, the server's status, and if you're using a proxy. Try increasing the timeout in Settings (gear icon → Settings → General → Request timeout).
  • SSL certificate errors. If you're testing a development server with a self-signed cert, you can turn off SSL verification in Settings (but do this only for testing, not production).
  • Getting 401 Unauthorized. Ensure you have the correct authentication token. Check the Authorization tab—select the type (e.g., Bearer Token) and paste the token. Also verify the token hasn't expired.
  • Response is not JSON. The server might be returning an error page (HTML). Check the response body in Raw view. Also ensure the request URL is correct and the server is not behind a WAF that blocks Postman.
  • Tests are not running. Make sure you've placed the test script in the Tests tab, not in Pre-request Script. Also check the Postman Console for JavaScript errors.

Frequently Asked Questions

Can I test APIs without writing any code?

Yes. You can send requests and visually inspect responses without scripting. However, writing simple test scripts helps automate validation and is recommended for repeatable tests.

How do I test an API that requires authentication?

Use the Authorization tab in Postman. Select the appropriate type (e.g., Bearer Token, Basic Auth, OAuth 2.0) and provide the required credentials. You can also store tokens in environment variables.

Can I run Postman tests in CI/CD?

Yes. Use Newman, Postman's command-line runner, to execute collections in your pipeline. Install via npm (npm install -g newman) and run newman run collection.json.

How do I test file uploads?

In the request Body tab, select form-data. Add a key with type File, then click Choose Files to select a file. Ensure the endpoint expects multipart/form-data.

What does the 'pm' object do?

pm is Postman's global object that provides methods to access response data, set variables, and define tests. For example, pm.response.json() parses the response body as JSON.

Can I test GraphQL APIs with Postman?

Yes. Send a POST request with the GraphQL query in the body as JSON: {"query": "{ users { id name } }"}. You can also use the GraphQL-specific body type in newer Postman versions.