Testing APIs with Postman

Get a brief introduction to the Postman tool and use it to manually test our first API.

Introduction

We will need to manually test and play with the API endpoints as they are coded. There are many tools out there that can do this. We will be using Postman, one of the leading tools, to demo various things throughout the course. You are free to use any other tool as long as the concepts are still clear.

There’s no need to install Postman for this course. All the executables in this course will take you to the following screen. Just click ‘Skip and go to the app’ in the left bottom each time.

Postman start screen
Postman start screen

The Postman app has a lot of features, so the dashboard page may seem a little overwhelming at first. Don’t worry, we will go step by step.

Workspaces

You may create different workspaces to manage multiple projects via Postman.

Collections

A collection is like a directory that can have one or more requests. We can add documentation, authorization settings, tests, and variables to a collection. We will start creating a ‘collection’ through the following steps:

Then, you can set the name of the Collection (for example: “Restaurant API”).

Postman variables

Variables are one of the advanced features of Postman, but they are helpful enough that we always suggest new users start using them from day one.

Variables also have scopes. For this course, we will use the collection variables to cover multiple requests. A collection is essentially a folder or directory that contains one or more requests.

In the following screenshot, we have set a couple of variables for our collection.

Postman will replace {{URL}} with the URL you have specified here before making the actual request.

Save the collection and request for future use.

Replace the initial value and current value of the URL variable with the following URL. This will directly test the API endpoints running on the Educative platform when you try various features of the web app online throughout the course.

http//:0.0.0.0:8000/api

Sample request

Below, we have prepared the request to call our first API endpoint that allows user registration. This is covered in the next lesson.

JSON as the interpreter

JSON is the most common data format that APIs use today. Almost all programming languages provide tools to play with JSON data. We will also be using JSON.

To inform Laravel that we’ll receive the respons JSON format, we need to pass a request header.

You can go to the ‘Headers’ tab and add the header, ‘Accept: application/json’. Here, Accept is the key and application/json is the value.

Sample response

Postman will be our travel “buddy” throughout this course, and we will learn about its advanced features at the end.

In the next lesson, we added some validation rules in the RegisterValidation class. Upon trying a request with the following data:

name: (blank)
email: (the one that already exists in the database)
password: 123456
password_confirmation: 456789

We should receive some errors with the HTTP status code that signals the type of response.

This should give you an idea of how to use Postman to test the API endpoints that we develop throughout the course.