CN
Piotr Jura·
April 29, 2023

Postman Basics

Introduction

When you're building REST APIs you need a way to test them - here comes Postman. The first step is to download Postman, get it here.

Making a Request with Postman

One of the fundamental features of Postman is making HTTP requests. To make a request, follow these simple steps:

  • Open Postman and click the '+' button to create a new tab.
  • Choose an HTTP method from the dropdown menu (GET, POST, PUT, DELETE, etc.).
  • Enter the API endpoint URL in the address bar.
  • Click 'Send' to execute the request.

HTTP Methods, Response Codes, and Headers

Postman supports all standard HTTP methods, including:

  • GET: Retrieve data from a server.
  • POST: Submit data to a server.
  • PUT: Update existing data on a server.
  • DELETE: Remove data from a server.

When you receive a response from the server, it will include an HTTP status code. These codes provide information about the outcome of your request. Some common status codes include:

  • 200 OK: Request was successful.
  • 201 Created: Resource was successfully created.
  • 400 Bad Request: Client-side error in the request.
  • 401 Unauthorized: Authentication is required.
  • 404 Not Found: Requested resource is not available.
  • 500 Internal Server Error: Server-side error.

Headers provide metadata about the request or response. Some frequently used headers include:

  • Content-Type: Indicates the media type of the resource.
  • Authorization: Contains credentials for authentication.
  • Accept: Specifies the media types the client accepts.
  • User-Agent: Identifies the client software making the request.

Query Parameters

Query parameters allow you to pass additional data to the server in the URL. They are usually key-value pairs that follow a question mark (?) in the URL. In Postman, you can easily add query parameters by clicking 'Params' next to the URL bar and entering the key-value pairs.

Workspaces, Environments, and Collections

Postman offers features to help you organize and streamline your API testing workflow:

  • Workspaces: Separate areas for organizing your projects. You can create personal or team workspaces to collaborate with others. Workspaces are super useful if you're working on multiple projects.
  • Environments: Sets of key-value pairs that allow you to switch between different setups (e.g., development, staging, production). You can have variables defined per environment (eg. BASE_URL). Makes it super easy to quickly switch from making a local request, to a making a request to the test server.
  • Collections: Groups of related API requests. Collections help you organize, document, and share your API requests with others. Every request that you make can be saved into a collection. Then it's easy to reproduce that request. Collections are super easy to share with others (eg. new team members).

Sharing in Postman: Export and Import

To export a collection or environment, simply right-click on it and choose 'Export.' Select the desired format (usually JSON) and save the file. To import, click the 'Import' button in the top left corner, and select the file you want to import.

Postman Automation with JavaScript

Let's explore a little trick to streamline the authentication process when working with Postman. By adding the following code snippet to your "Tests" tab on the sign-in request:

const json = JSON.parse(responseBody);
pm.environment.set("TOKEN", json.token);

You can automatically extract the authentication token from your API's response and save it as an environment variable.

This way, Postman will populate the auth token for every subsequent request, eliminating the need for manual token copying and pasting.