Skip to content

How To Send Http Request Using Postman

API Testing Blog

A Comprehensive Guide to Sending HTTP Requests Using Postman for API Testing

Postman is a powerful tool that allows you to test APIs by sending HTTP requests and inspecting the responses. This guide will provide you with a step-by-step walkthrough of how to send various types of HTTP requests using Postman, along with practical examples and sample codes to help you get started.

Sending an HTTP Request: A Basic Example

  1. Open Postman: Launch the Postman application or access the web version.
  2. Create a New Request: Click on the “New” button in the top-left corner to create a new request.
  3. Choose the Request Method: Select the appropriate HTTP method from the dropdown menu, such as GET, POST, PUT, DELETE, etc.
  4. Enter the Request URL: In the “Enter request URL” field, type the endpoint URL of the API you want to interact with.
  5. Add Headers: Optional: If the API requires specific headers, such as authorization tokens or content types, click on the “Headers” tab and add them as key-value pairs.
  6. Send the Request: Click the “Send” button to send the request to the API server.
  7. Inspect the Response: The response from the API will be displayed in the “Body” tab, including the status code, headers, and response content.

Sample Code:

// Example GET request to retrieve data from a public API
// Request URL: https://reqres.in/api/users
GET https://reqres.in/api/users

Sending a POST Request with a JSON Body

  1. Choose the POST Method: Select “POST” from the HTTP method dropdown.
  2. Set the Content Type Header: In the “Headers” tab, add a header with the key “Content-Type” and the value “application/json”.
  3. Enter the Request Body: In the “Body” tab, select the “raw” option and choose “JSON” as the format.
  4. Enter JSON Data: Write your JSON payload in the editor.

Sample Code:

// Example POST request to create a new user
// Request URL: https://reqres.in/api/users
POST https://reqres.in/api/users
Content-Type: application/json
{
"name": "John Doe",
"job": "QA Engineer"
}

How to send an HTTP Request using Postman with Parameters

  1. Add Parameters to the URL: Append query parameters to the end of your request URL using the format “?key1=value1&key2=value2”.
  2. Add Parameters in the Body: For POST, PUT, or PATCH requests, you can include parameters within the request body using JSON format.

Sample Code:

// Example GET request with query parameters
// Request URL: https://reqres.in/api/users?page=2
GET https://reqres.in/api/users?page=2

Understanding HTTP Response Codes

  • 200 OK: Request successful.
  • 201 Created: Resource created successfully.
  • 400 Bad Request: The request was invalid.
  • 401 Unauthorized: The request was not authorized.
  • 404 Not Found: The resource was not found.
  • 500 Internal Server Error: An error occurred on the server.

Sending Multiple Requests: Collections and Environments

Collections:

  • Organize related requests into logical groups.
  • Share and collaborate on API tests with your team.
  • Use variables to reuse values across requests.

Environments:

  • Store global variables like API keys, base URLs, and environment-specific settings.
  • Easily switch between different environments (e.g., development, testing, production).

Testing with Assertions: Validating API Responses

Postman allows you to write tests using Javascript to validate API responses. This enables you to:

  • Verify the status code of the response.
  • Assert the presence and values of specific fields in the response.
  • Ensure the response meets your expected format.

Sample Code:

// Test script for a successful response and checking the value of "name"
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Name should be 'John Doe'", function () {
pm.expect(pm.response.json().name).to.eql("John Doe");
});

How to Send HTTP Request using Postman with Authorization

Many APIs require authentication to access resources. Postman provides different authorization methods:

  • Basic Auth: Simple authentication using username and password.
  • Bearer Token: Authenticate using a JWT or other access token.
  • OAuth 2.0: A more complex authentication protocol commonly used for web applications.
  • API Key: Authenticate using a unique API key provided by the service.

Sample Code:

// Example using Bearer Token authorization
// Request URL: https://example.com/api/protected-resource
GET https://example.com/api/protected-resource
Authorization: Bearer your_access_token

Debugging and Troubleshooting

  • Use the console tab to view any errors or messages from your test scripts.
  • Enable request logging to capture the complete details of each request and response.
  • Utilize the Postman debugger to step through your test scripts and examine their execution.

By learning how to send HTTP requests effectively using Postman and understanding the basics of API testing, you can streamline your testing process, ensure the quality of your APIs, and confidently deploy your applications.

API Testing Blog