Skip to content

How To Use Postman To Post

API Testing Blog

Using Postman to Send POST Requests for API Testing

Postman is a powerful tool for interacting with APIs, and sending POST requests is a core functionality. In this guide, we’ll walk through the fundamental steps of crafting and executing POST requests using Postman, with practical examples to illustrate the concepts.

1. Creating a New POST Request

  1. Open Postman: Launch the Postman application.
  2. Create a New Request: Click on the “New” button in the top left corner of the application.
  3. Select “POST”: Choose the “POST” method from the dropdown menu next to the request URL field.

2. Setting the Request URL

  1. Enter the Target URL: In the request URL field, type the complete address of the API endpoint you want to interact with. For instance, if you’re testing a user registration API, it might look like this: https://api.example.com/users.

3. Adding Request Headers

  1. Headers Tab: Navigate to the “Headers” tab in the Postman request window.
  2. Key-Value Pairs: Enter headers as key-value pairs. Common headers include:
    • Content-Type: Specifies the format of the request body (e.g., application/json, application/xml).
    • Authorization: Used for authentication, containing information like tokens or API keys.

Example:

Content-Type: application/json
Authorization: Bearer your_access_token

4. Constructing the Request Body

  1. Body Tab: Switch to the “Body” tab.
  2. Select Body Type: Choose the appropriate body type:
    • raw: For plain text, JSON, XML, or other formats.
    • form-data: For sending form-like data (key-value pairs) with file uploads.
    • x-www-form-urlencoded: For sending form-like data as URL-encoded key-value pairs.
    • binary: For sending binary data.

Example (JSON): Let’s create a sample JSON body for registering a new user:

{
"username": "john.doe",
"email": "john.doe@example.com",
"password": "P@$$wOrd"
}

5. Sending the POST Request

  1. Send Button: Click on the “Send” button to execute the POST request.

6. Examining the Response

  1. Response Body: The response from the API will be displayed in the “Body” section of the Postman window.
  2. Response Headers: View the server’s response headers in the “Headers” tab.
  3. Status Code: Pay close attention to the HTTP status code displayed in the response (e.g., 201 Created, 400 Bad Request).

Using Postman to Post with Authorization

1. Authentication Types in Postman

Postman provides several authentication mechanisms:

  • Basic Auth: Sending username and password in the request header.
  • Bearer Token: Using an access token (usually JWT) to authorize requests.
  • API Key: Passing a unique API key as a header or query parameter.
  • OAuth 1.0: Using consumer keys, access tokens, and secret keys.
  • OAuth 2.0: Using a similar process as OAuth 1.0 but with some differences.

2. Setting Up Authentication in Postman

  1. Go to the “Authorization” tab in the Postman request window.
  2. Select Authentication Type: Choose the appropriate authentication method from the provided options.
  3. Provide Credentials: Enter the required authentication information (username/password, token, API key, etc.) based on the selected method.

Example (Bearer Token):

  1. Select “Bearer Token” as the authentication type.
  2. Paste your access token into the “Token” field.

3. Sending the Authorized POST Request

  1. Send: Click the “Send” button to execute the request.
  2. Inspect Response: Verify the response, checking the status code and the body for successful execution.

Advanced Postman Usage for POST Requests

1. Using Environment Variables

Environment variables allow you to store and reuse API endpoints, authentication credentials, and other dynamic values across multiple requests. They make your Postman collections more maintainable and flexible.

  1. Manage Environments: Go to the “Environments” in the Postman interface.
  2. Create an Environment: Create a new environment and add variables (e.g., API_BASE_URL, ACCESS_TOKEN).
  3. Reference in Requests: Use ${variable_name} syntax to reference environment variables within your POST requests. For example: ${API_BASE_URL}/users.

2. Test Scripts for POST Requests

Postman’s scripting functionality allows you to automate validation checks, data extraction, and other complex operations.

Example (Test Script):

pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("Response body contains new user ID", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.id).to.be.a('number');
});

3. Using Collections

Organize related Postman requests into collections. Collections make it easy to group tests, share them with others, and run them as a suite.

  1. Create a Collection: Go to “Collections” and click “Create Collection”.
  2. Add Requests: Add your POST requests to the collection.
  3. Run Collection: Use the “Run” button to execute all requests within the collection.
  4. View Results: The “Test Results” tab displays the execution reports and test assertions.

API Testing Blog