Skip to content

How To Use Postman On Localhost

API Testing Blog

Setting Up Your Environment

Before you get started, ensure you have the following:

  • Postman: Download and install Postman from https://www.postman.com/.
  • A local server running an API: This could be a simple Node.js server, a Python Flask app, or any other backend framework you’re comfortable with.

Creating a New Request

  1. Open Postman: Launch the Postman application.
  2. Create a new request: Click on the “New” button in the top left corner and select “Request.”

Configuring the Request

  1. Enter the URL: In the address bar (top of the screen), type in the URL of your API endpoint. Since you’re targeting localhost, the URL will usually start with http://localhost:port_number/. Example: http://localhost:3000/users.
  2. Select the HTTP Method: Choose the appropriate HTTP method from the dropdown menu (GET, POST, PUT, DELETE etc.).
  3. Set Headers (Optional): If your API requires specific headers (e.g., authorization), click on the “Headers” tab and add your headers.

Sending Your Request

  1. Send the Request: Click the “Send” button to send the request to your local server.
  2. View the Response: Postman will display the response from your server in the “Body” tab. This includes the response status code (200 for success, 404 for not found, etc.), headers, and the response data.

Practical Example: GET Request

Let’s say you have a simple Node.js server running on http://localhost:3000 with an endpoint /users that retrieves a list of users.

Step 1: Set up your server

const express = require('express');
const app = express();
app.get('/users', (req, res) => {
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];
res.json(users);
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});

Step 2: Configure your Postman request

  • URL: http://localhost:3000/users
  • Method: GET

Step 3: Send the request and view the response

After sending the request, you should see a response with a 200 status code and the following data in JSON format:

[
{
"id": 1,
"name": "Alice"
},
{
"id": 2,
"name": "Bob"
}
]

Working with POST Requests

For POST requests, you need to provide data in the request body.

Step 1: Set up your server (Node.js example)

app.post('/users', (req, res) => {
const newUser = req.body;
// ... (Logic to create the user)
res.status(201).json({ message: 'User created successfully' });
});

Step 2: Configure your Postman request

  • URL: http://localhost:3000/users
  • Method: POST
  • Body: Select “raw” and choose JSON as the format.
    {
    "name": "Charlie"
    }

Step 3: Send the request and view the response

You’ll receive a response with a 201 status code (Created) and a success message.

Using Postman Collections

For organizing and managing multiple requests, Postman Collections are a powerful tool.

  1. Create a new collection: Click the “New” button and select “Collection.” Give it a name (e.g., “My API Tests”).
  2. Add requests to the collection: Drag and drop individual requests into the collection or create new requests within the collection.
  3. Use environment variables: Define environment variables for things like base URLs or authentication tokens. This allows you to quickly change these values without modifying each individual request.

Using Postman Environments

Environments allow you to manage different settings for various environments (development, testing, production).

  1. Create a new environment: Click the “New” button and select “Environment.”
  2. Add variables: Create variables specific to your different environments (e.g., apiUrl, authToken).
  3. Select the environment: Before sending a request, select the appropriate environment to use its variables.

Testing and Validation

Postman offers built-in features for testing your API’s functionality and validating the responses:

  • Tests Tab: Write simple JavaScript tests to verify the response data, status codes, headers, etc.
  • Assertions: Use Postman’s built-in assertions (e.g., pm.test("Status code is 200", function () { pm.response.to.have.status(200); })) to automatically validate your requests.

Conclusion

Postman simplifies the process of testing APIs hosted on localhost by offering a user-friendly interface, versatile functionality, and comprehensive tools for testing and validation. With its robust features and intuitive design, Postman empowers developers and testers to streamline their API testing workflows and ensure the quality and reliability of their applications.

API Testing Blog