Testing REST APIs with Postman

Tutorial 1 of 5

1. Introduction

Goal of the Tutorial

This tutorial aims to guide you through the process of testing REST APIs using Postman, a popular API testing tool.

Learning Outcomes

By the end of this tutorial, you will:
- Understand how to set up Postman for testing REST APIs.
- Be able to make GET, POST, PUT, and DELETE requests.
- Learn how to validate responses.

Prerequisites

Familiarity with the basics of REST APIs and HTTP methods (GET, POST, PUT, DELETE) is beneficial but not mandatory.

2. Step-by-Step Guide

Concept Explanation

Postman is a tool that lets us send requests to an API and receive responses. It simplifies API testing and provides a user-friendly interface to interact with APIs.

Examples

We will be using a free, public API for this tutorial: JSONPlaceholder.

Best Practices and Tips

Remember to always check the status code and the response body when testing an API. Different status codes indicate different responses, and the response body should match what you expect.

3. Code Examples

Making a GET Request

  1. Open Postman and click on the '+' button to open a new tab.
  2. In the dropdown next to the URL bar, select 'GET'.
  3. Enter the URL: https://jsonplaceholder.typicode.com/posts.
  4. Click 'Send'.
  5. The response body below should show a list of posts, and the status code should be '200 OK'.

Making a POST Request

  1. In a new tab, select 'POST' and enter the same URL.
  2. Under 'Body', select 'raw' and 'JSON'.
  3. Enter the following JSON:
{
  "title": "foo",
  "body": "bar",
  "userId": 1
}
  1. Click 'Send'.
  2. The response should include the same JSON you sent, with a new 'id' field, and the status should be '201 Created'.

4. Summary

In this tutorial, we learned how to set up Postman and use it to test REST APIs. We made GET and POST requests and checked the responses.

Next Steps

Now that you've mastered the basics, try making PUT and DELETE requests to the API.

Additional Resources

  1. Postman Learning Center
  2. HTTP Status Codes

5. Practice Exercises

  1. Make a PUT request to https://jsonplaceholder.typicode.com/posts/1 with the following JSON:
{
  "id": 1,
  "title": "foo",
  "body": "bar",
  "userId": 1
}

Check that the response includes the same JSON and the status is '200 OK'.

  1. Make a DELETE request to https://jsonplaceholder.typicode.com/posts/1.
    Check that the response body is empty and the status is '200 OK'.

Solutions

  1. The PUT request updates an existing resource, so the server should return the updated resource and a '200 OK' status code.
  2. The DELETE request deletes a resource. The server should return an empty body and a '200 OK' status code.

Further Practice

Try testing different APIs and see how they handle different requests. Always remember to check the documentation for any specific requirements.