Postman API testing by example

Postman API testing by example


Postman is a tool to help you develop APIs. Postman helps you build APIs by providing tools to capture, validate, and test requests and responses. API testing is the process of verifying that your Application Programming Interface (API) is working correctly. This article will use Postman & Javascript for API testing.


Table of Contents



Looking for a Postman alternative?

Testfully's free plan offers Unlimited team members, Multi-step tests, team collaboration, historical test results and many more features. Import from Postman supported!

Postman uses Javascript for API testing & API monitoring. If you don’t know how to code in Javascript or prefer a no-code API testing tool, we highly recommend reading our top API testing tools article to learn more about No Code API testing tools.


Test API status code using Postman


If a user wants to interact with the API, they will send an HTTP request to the API endpoint, and depending on the action and content of the request, the server responds with the appropriate status code. We can define an HTTP response as either a success or an error depending on whether or not the request was successful.

Checking the response status code is one way to test an API. The below code snippets verify the HTTP response status code in Postman.


The table below lists pm fields with information related to the response status code:

Field Description Example
pm.response.status Status Text OK
pm.response.code Status Code 200


Postman test to check whether status is 200 OK


pm.test("the endpoint returns the expected status code", () => {
  // change 200 to the response code you expect
  const expectedStatusCode = 200;

  pm.response.to.have.status(expectedStatusCode);
});


Postman test to check whether status is 200 or 201


pm.test("the endpoint returns the expected status code", () => {
  // comma separate the valid response codes below
  const expectedStatusCodes = [200, 201];

  pm.expect(pm.response.code).to.be.oneOf(
    expectedStatusCodes,
    `expected response status to be one of ${expectedStatusCodes} but got ${pm.response.code}.`
  );
});


Postman test to check status code is not 404


pm.test("the endpoint does not return unexpected status code", () => {
  // change 404 to the response code you do not expect
  const expectedStatusCode = 404;

  pm.response.to.not.have.status(expectedStatusCode);
});


Postman test to check status is not 404 or 500


pm.test("the endpoint does not return unexpected status codes", () => {
  // comma separate the valid response codes below
  const unexpectedStatusCodes = [404, 500];

  pm.expect(pm.response.code).to.not.be.oneOf(
    unexpectedStatusCodes,
    `did not expect response status to be one of ${unexpectedStatusCodes} but got ${pm.response.code}.`
  );
});


Test API response time using Postman


The API response time is an important metric to test, measuring how long it takes for the API to respond to requests. Slow response times can lead to poor user experience and can be severely affected by peak traffic conditions. This section shows you how to test response time in Postman.


The table below lists pm fields with information related to response time:

Field Description Example
pm.response.responseTime Response time (in milliseconds) 255


Postman test to check response time


pm.test("API responds within the expected treshhold", () => {
  // set the response time in milliseconds
  const expectedTimeInMilliseconds = 500;

  pm.expect(pm.response.responseTime).to.be.lessThan(
    expectedTimeInMilliseconds + 1,
    `The endpoint did not respond within ${expectedTimeInMilliseconds} ms. Response came in ${pm.response.responseTime} ms`
  );
});


Test API response body using Postman


API response has a body, which is the return value of the operation. The response body is returned in a JSON- or XML-encoded string. The response body defines the structure and content of the response payload.


The table below lists pm fields and functions with information related to the response body:

Field / Function Description Example
pm.response.json() Response body (Javascript object) {"hello": "world"}
pm.response.body Response body (text) {"hello": "world"}


Postman test to check field value in response


We can validate the value of both id and name fields of the https://rickandmortyapi.com/api/character/2 using the test below.

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the id field is 2 (number).
  pm.expect(response).to.have.property("id", 2);

  // the line below checks value of the name field is Morty Smith (string).
  pm.expect(response).to.have.property("name", "Morty Smith");
});


Postman test to check nested field value in response


The script below step works for fields at the root of the response. What if we wanted to test the name field under the origin field. We can tweak the script a little bit to support fields at any level.

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the id field is 2 (number).
  pm.expect(response).to.have.nested.property("id", 2);

  // the line below checks value of the name field is Morty Smith (string).
  pm.expect(response).to.have.nested.property("name", "Morty Smith");

  // the line below checks value of the origin.name field is Earth (C-137) (string).
  pm.expect(response).to.have.nested.property("origin.name", "Earth (C-137)");
});

To check value of nested fields, provide the path (from root) to the field by chaining field names using dot (.)


Postman test to check nested array value in response


We can take it even further and use the same technique to validate the value of items in the array. For example, we can use the below script to check the value of the second item in the episode array of the https://rickandmortyapi.com/api/character/2 endpoint.

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks the value of the origin.name field is Earth (C-137) (string).
  pm.expect(response).to.have.nested.property("episode.1", "https://rickandmortyapi.com/api/episode/2");
});

You may have noticed, but to access the second item in the episode list, we used the number 1; why? In Javascript, the position of the items in lists (array) starts from 0, so the first item has 0 as the position number, the second one 1, and so on.


Test API response headers using Postman


When you hit an API endpoint, one or more HTTP header is returned, along with the data from the call. This header contains information about the API endpoint that was called and can be used by API clients to understand better the call that was made.


The table below lists pm fields and functions with information related to response headers:

Field Description
pm.response.headers Response heaaders


Postman test to check response header


pm.test("API response contians the expected header", () => {
  pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});


No code API testing using Testfully


Testfully offers a generous free plan

Testfully is a leading No Code API testing & monitoring tool and a great Postman alternative for API testing. The below video is a quick demo of Testfully and how you can use it to test your APIs without writing code.

comments powered by Disqus

Recent Articles

blog-image
7 HTTP methods and how to use them

HTTP protocol works by clients sending requests to the servers and servers responding to the requests. We do CRUD operations (Create, Read, Update, Delete) by sending HTTP requests with different HTTP methods, sometimes called HTTP verbs.



blog-image
Introduction to API Blueprint

API blueprint is a powerful high-level API design language for web APIs. In this article, we want to dive deeper into it and learn more about how it works, the differences between API blueprint and Swagger, and what makes it unique that leads to its extensive use. But before we dig into API Blueprint, we must ensure a solid base of information about the “API first approach” concepts.

blog-image
False positive & false negative in software testing

Exports in automated software testing have borrowed false positive and false negative terms from the medical examination field. In the medical field, the purpose of a test is to determine whether the patient has a particular medical condition or not. As far as software testing is concerned, a false positive indicates a bug when there is none. Conversely, a false negative indicates no bug when there is one.