Skip to content

How To Run Api Using Postman

API Testing Blog

How to Run an API Using Postman

Postman is a powerful tool used for testing and interacting with APIs. It allows you to send requests, view responses, manage API collections, and even automate your testing process. This guide will walk you through the fundamentals of using Postman to run API requests.

Getting Started with Postman

  1. Download and install Postman: You can download Postman for free from their official website: https://www.postman.com/.
  2. Create an account (optional): Creating a free Postman account lets you save your requests, collections, and environments, making it easier to collaborate with others.
  3. Launch the application: Once installed, open Postman. You should see the main interface with the “New” button on the left side.

Sending Your First Request

  1. Select the request method: In the top left corner, you’ll see a dropdown to choose the HTTP method for your request. For example, you can choose “GET” to retrieve data or “POST” to submit data to an API.
  2. Enter the API endpoint URL: In the address bar, type the URL of the API endpoint you want to interact with. For instance, you might enter https://api.example.com/users.
  3. Add headers (optional): Many APIs require you to include headers with your requests. Click on the ‘Headers’ tab and add key-value pairs for the required headers. This might include authentication tokens, content type, or other information.
  4. Add a body (optional): For methods like “POST”, “PUT”, or “PATCH”, you’ll often need to send data in the request body. Click on the ‘Body’ tab and choose the appropriate format (e.g., JSON, XML) for your body data.
  5. Send the request: After configuring the request, click the ‘Send’ button.

Understanding the Response

Once you send the request, Postman displays the API response in different tabs:

  • Body: Shows the content returned by the API. You can view this content in various formats, such as JSON, HTML, or text.
  • Headers: Contains the headers returned by the API.
  • Cookies: Lists any cookies set by the API.
  • Test: Allows you to write automated tests to verify the response data.

Example: Making a GET Request

Let’s say you want to retrieve a list of users from an API. Here’s how you could do it using Postman:

  1. Method: Select “GET” from the HTTP method dropdown.
  2. URL: Enter the API endpoint: https://api.example.com/users
  3. Headers: If the API requires authentication, you would add headers for authorization here.
  4. Send: Click the “Send” button.

Postman will send the GET request to the specified API endpoint. The response will be displayed in the different tabs.

Example: Making a POST Request

Imagine you want to create a new user account. You’ll need to use a “POST” request to send the user information to the API.

  1. Method: Select “POST.”
  2. URL: Enter the endpoint: https://api.example.com/users
  3. Headers: Set the “Content-Type” header to “application/json.”
  4. Body: In the ‘Body’ tab, select ‘raw’ and choose “JSON” as the format:
{
"username": "john.doe",
"email": "john.doe@example.com"
}
  1. Send: Click the “Send” button.

Postman will send the POST request along with the user data in the JSON format. The API will respond with information about the newly created user.

Using Postman Collections and Environments

Postman Collections allow you to organize related API requests, making it easier to manage and reuse them. Environments provide a way to store variables used in your requests, like API keys or base URLs.

  • Create a collection: Click the “New” button and choose ‘Collection.’ Give your collection a name and description.
  • Add requests to your collection: Click the “Add Request” button in your collection and configure the request as described earlier.
  • Create an environment: Go to the ‘Environments’ tab and click on ‘Add Environment.’
  • Define variables: Add key-value pairs for variables that you want to use in your requests. You can reference these variables in your requests using double curly braces (e.g., {{baseUrl}}).

These features let you manage and reuse API requests efficiently, particularly when working with multiple APIs or environments.

Testing Your APIs with Postman

Postman provides built-in features for testing your APIs:

  • Test Scripts: You can write JavaScript code within the ‘Test’ tab to validate the API response data, check status codes, and perform other assertions.

Example Test Script:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response has a user with username 'john.doe'", function () {
pm.expect(pm.response.json().find(user => user.username === 'john.doe')).to.be.ok;
});
  • Pre-Request Scripts: These scripts execute before the request is sent. You can use them to set up variables or modify the request data.

Example Pre-Request Script:

pm.environment.set("username", "john.doe");
pm.environment.set("email", "john.doe@example.com");
pm.request.body.raw = JSON.stringify({
username: pm.environment.get("username"),
email: pm.environment.get("email")
});

Postman’s testing features allow you to ensure your API’s functionality and provide robust documentation for your API endpoints.

Conclusion

Postman is a powerful tool that simplifies interacting with APIs, from sending requests to writing automated tests. By mastering these basic concepts, you can leverage Postman to effectively test your APIs, ensure their functionality, and accelerate your development workflow.

API Testing Blog