Skip to content

How To Use Postman To Send Json Request

API Testing Blog

How to Use Postman to Send JSON Requests: A Comprehensive Guide

Postman is a powerful tool for API testing, and sending JSON requests is a fundamental part of the process. This guide will walk you through the steps of crafting and sending JSON requests using Postman, equipping you with the knowledge to effectively interact with APIs.

1. Creating a New Request

  1. Launch Postman: Open your Postman application.
  2. Create a Request: Click on the “New” button in the top left corner and choose “Request.”
  3. Choose HTTP Method: Select the appropriate HTTP method for your API request. Common methods include:
    • GET: Retrieves data from the server.
    • POST: Sends data to the server to create a new resource.
    • PUT: Updates an existing resource on the server.
    • DELETE: Deletes a resource from the server.
  4. Enter Request URL: In the “Enter request URL” field, provide the complete URL of the API endpoint you want to interact with.

Example: Let’s say we are working with a fictional API for managing books. The base URL for the API might be https://bookapi.example.com/v1. To access the endpoint for fetching a specific book, we would append /books/123 to the base URL.

2. Sending a Simple JSON request using GET method

  1. Set the HTTP Method: Select “GET” as the HTTP method.
  2. Enter Request URL: Input the complete URL for the API endpoint, for example: https://bookapi.example.com/v1/books/123.
  3. Send the Request: Click on the “Send” button to execute the request.

Example: Fetching a book details

{
"id": 123,
"title": "The Lord of the Rings",
"author": "J.R.R. Tolkien",
"genre": "Fantasy"
}

3. Sending a JSON Request with Body Data

For methods like POST, PUT, or PATCH, you need to send data in the request body. Here’s how to do that with JSON:

  1. Choose the HTTP Method: Select the appropriate method based on your API interaction, like POST for creating a new resource.
  2. Enter Request URL: Provide the complete URL for the API endpoint.
  3. Select Body Tab: Click on the “Body” tab in the request builder.
  4. Choose ‘raw’ Option: Select “raw” as the body type.
  5. Select JSON as the application/json: From the dropdown, select “JSON” as the content type.
  6. Paste/Enter JSON Data: Enter or paste your JSON data into the text editor.

Example: Creating a new book

{
"title": "The Hobbit",
"author": "J.R.R. Tolkien",
"genre": "Fantasy"
}

4. Using Parameters with JSON Requests

  1. Choose the HTTP Method and URL: Select the HTTP method (GET, POST, etc.) and enter the URL.
  2. Add Parameters: Click on the “Params” tab in the request builder.
  3. Enter Key-Value Pairs: Enter the parameter names and their corresponding values, formatted as key-value pairs.

Example: Filtering Books by Genre

{
"genre": "Fantasy"
}

You can use the Params tab to add query parameters for GET requests, or you can include parameters directly in the URL itself.

5. Using Headers for JSON Requests

JSON requests often require specific headers to indicate the type of data being sent:

  1. Open Headers Panel: Click on the “Headers” tab within the request builder.
  2. Add Header Fields: Enter the header name and its corresponding value. You can use Content-Type to specify that you are sending JSON data:
    • Content-Type: application/json

Example:

{
"Content-Type": "application/json"
}

6. Authorization and Authentication

Many APIs require authorization or authentication to access resources. Postman allows for various methods:

  1. Basic Auth: Click on the “Authorization” tab and select “Basic Auth.” Enter your username and password.
  2. Bearer Token: Select “Bearer Token” and provide the necessary token.
  3. API Keys: Choose “API Key” and specify the header name and key value.

Example: Authorization with Bearer Token

{
"Authorization": "Bearer your_token_here"
}

7. Verifying Expected Responses

Postman makes it easy to verify that your API requests are returning the expected responses:

  1. Response Tab: After sending your request, navigate to the “Response” tab.
  2. Status Code Verification: Ensure the response status code matches your expectations. For example, 200 (OK) for successful requests, 404 (Not Found) for missing resources, etc.
  3. Response Body Analysis: Examine the response body. If you sent a JSON request, you’ll receive JSON data. You can use Postman’s built-in “Pretty” feature to format the JSON for easy reading.
  4. Assertions: Postman has a powerful feature called “Assertions” that lets you write code to verify specific aspects of the response, like the status code, headers, or content of the response body. Use them to create automated tests for your API.

Example: Assertions

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

This assertion verifies that the response status code is 200. You can write additional assertions to check for other things like the existence of specific data within the response body.

8. Creating and Managing Collections

Postman allows you to organize your API requests into collections:

  1. Create a New Collection: Click on the “New” button in the top left corner and choose “Collection.”
  2. Add Requests: Drag and drop requests from your workspace into the collection folder.
  3. Run Collections: You can run all requests within a collection in a specified order, allowing for efficient API testing.

Using Postman for Efficient API Testing

By mastering the techniques outlined in this guide, you can leverage Postman’s capabilities to send JSON requests, test various API endpoints, and efficiently validate expected responses. Remember, Postman offers a wide array of features beyond the basics covered here. As you delve deeper, you will discover advanced features such as environment variables, data-driven testing, and more. Embrace Postman as a powerful tool in your API testing arsenal, enabling efficient development and quality assurance for your projects.

API Testing Blog