Skip to content

How To Use Postman From Localhost

API Testing Blog

Setting Up the Environment

Before you can use Postman to test your local API, you need to ensure that your local server is running and accessible. Here’s a basic example using Node.js and Express:

1. Create a simple Node.js Server (using Express)

const express = require('express');
const app = express();
const port = 3000;
app.get('/users', (req, res) => {
res.json([
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Jane Doe' }
]);
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});

2. Start the server:

Terminal window
node index.js

This starts a server that will respond to requests at http://localhost:3000/users.

How to Make Requests to Your Localhost Server using Postman

1. Install Postman:

2. Create a New Request:

  • Open Postman.
  • Click on the “New” button in the top left corner.
  • Select “Request” from the dropdown.

3. Configure the Request:

  • Method: Choose the HTTP method you want to use. For example, GET for retrieving data.
  • URL: Enter the full URL of your API endpoint. In our example, it would be http://localhost:3000/users
  • Headers: (Optional) Add any custom headers required by your API.

4. Send the Request:

  • Click the “Send” button at the top right of the interface.

5. View the Response:

  • Postman will display the response from your server. This includes the status code, headers, and body of the response.

Example of using Postman for GET request:

  • Method: GET
  • URL: http://localhost:3000/users
  • Headers: (None in this case)

Response:

[
{ "id": 1, "name": "John Doe" },
{ "id": 2, "name": "Jane Doe" }
]

Working with Other HTTP Methods

Postman supports all standard HTTP methods:

1. POST: Used for sending data to the server, for example, creating a new user:

  • URL: http://localhost:3000/users
  • Method: POST
  • Body: JSON payload:
    {
    "name": "Alice Smith"
    }
  • Headers: Content-Type: application/json

2. PUT: Used for updating existing data, for example, changing a user’s name:

  • URL: http://localhost:3000/users/1
  • Method: PUT
  • Body: JSON payload:
    {
    "name": "Alice Jones"
    }
  • Headers: Content-Type: application/json

3. DELETE: Used for deleting data:

  • URL: http://localhost:3000/users/1
  • Method: DELETE

Using Postman Collections for API Testing

1. Create a New Collection:

  • Click the “Create Collection” button in Postman.
  • Give your collection a name (e.g., My Local API).

2. Add Requests to the Collection:

  • Within your collection, create new requests for each endpoint you need to test.

3. Organize Requests with Folders:

  • If your API has a lot of endpoints, use folders to group them logically. For example, you might have folders for Users, Products, etc.

4. Add Tests:

  • Postman allows you to write test scripts (using JavaScript) to automate checks against your API responses. These can verify:
    • Status codes (e.g., asserting a 200 for successful requests)
    • Response data (e.g., verifying that the user name is correct)
    • Response headers (e.g., checking for specific headers)

Sample Test Script:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response body has a name property", function () {
pm.response.to.have.jsonBody("name");
});

5. Run Collections:

  • You can run your entire collection with a single click, executing all the requests and tests in order. This helps with:
    • Automated Testing: Catch bugs early and ensure your API remains consistent.
    • Regression Testing: Run tests regularly to guarantee your changes don’t break existing functionality.

Debugging and Troubleshooting

  • Check Server Logs: Examine your server’s console output for errors or warnings that might shed light on why requests are failing.
  • Inspect Network Tab: Using your browser’s “Network” tab (usually accessible through the developer tools), you can see detailed information about each request and response, including headers, request bodies, and response times. This helps pinpoint issues with network connectivity or request/response data.
  • Use Postman’s Debugging Features:
    • Console: Postman has a built-in console for logging messages and evaluating JavaScript expressions, which is very helpful for debugging test scripts.
    • “Test” Tab: Utilize the “Test” tab to inspect code execution and make step-by-step checks of your test scripts.

Key Benefits of Using Postman for Local API Testing

  • User-Friendly Interface: Postman simplifies the process of making requests and viewing responses.
  • Powerful Request Building: Easily create complex requests with custom headers, parameters, and authorization.
  • Testing Automation: Write test scripts to easily automate API tests and ensure consistency.
  • Collaboration & Sharing: Postman allows you to collaborate with others by sharing collections and environments.
  • Environment Management: Organize your test environments (e.g., development, staging, production) for seamless testing across different environments.

API Testing Blog