API Testing and examples with Postman
Introduction
API testing is an essential part of ensuring backend functionality and data integrity within applications. This course covers the four main HTTP methods used in API testing: PUT, POST, GET, and DELETE. Using Postman, a popular API testing tool, you’ll learn how to construct and send requests, verify responses, and understand how each method serves a different purpose in CRUD operations.
1. HTTP Methods Overview
- GET: Retrieve data from the server. For example, fetching a list of users.
- POST: Submit new data to the server. This method is commonly used to create new resources, such as adding a new user.
- PUT: Update an existing resource. It replaces the current representation of the target resource with the provided data.
- DELETE: Remove data from the server. This action is used to delete resources, such as removing a user.
2. Setting Up Postman
- Download and Install Postman: Visit Postman to download the latest version.
- Create a New Request: Open Postman, then click on New > Request. You can name your request and add it to a collection.
- Enter the API Endpoint: In the request setup, enter the URL for the API you’re testing.
3. API Testing Example
To demonstrate each method, we’ll use a sample endpoint, assuming the base URL https://jsonplaceholder.typicode.com
(a popular mock API for testing).
A. GET Request
- Set up the GET Request:
- URL:
https://jsonplaceholder.typicode.com/posts/1
- This request fetches data from the server for the post with ID
1
.
- URL:
- Send the Request:
- Click on Send. You should see a JSON response with the details of the post.
- Validate the Response:
- Verify that the response status is
200 OK
and contains expected data.
- Verify that the response status is
B. POST Request
- Set up the POST Request:
- URL:
https://jsonplaceholder.typicode.com/posts
- Method: POST
- Body: Choose raw and select JSON format.
{ "title": "foo", "body": "bar", "userId": 1 }
- URL:
- Send the Request:
- Click on Send. You should see a response with a
201 Created
status code and the new resource in the response.
- Click on Send. You should see a response with a
- Validate the Response:
- Check that the response contains the data you sent and a new
id
.
- Check that the response contains the data you sent and a new
C. PUT Request
- Set up the PUT Request:
- URL:
https://jsonplaceholder.typicode.com/posts/1
- Method: PUT
- Body: Choose raw and select JSON format.
{ "id": 1, "title": "updated title", "body": "updated body", "userId": 1 }
- URL:
- Send the Request:
- Click on Send. The response should indicate that the post has been updated.
- Validate the Response:
- Confirm that the
title
andbody
in the response match the updated data.
- Confirm that the
D. DELETE Request
- Set up the DELETE Request:
- URL:
https://jsonplaceholder.typicode.com/posts/1
- Method: DELETE
- URL:
- Send the Request:
- Click on Send. The response should show a
200 OK
or204 No Content
status code.
- Click on Send. The response should show a
- Validate the Response:
- Verify that the resource was deleted by sending another GET request to the same URL. You should receive a
404 Not Found
response.
- Verify that the resource was deleted by sending another GET request to the same URL. You should receive a
4. Summary
In this course, you learned the fundamental HTTP methods for API testing using Postman. Each method plays a specific role in interacting with server resources:
- GET to retrieve data
- POST to create new data
- PUT to update existing data
- DELETE to remove data
With these methods and Postman, you have the tools to test and validate API behavior, ensuring that it meets your application’s requirements.
Quiz: API Testing Knowledge Check
-
Which HTTP method is used to retrieve data from a server?
- GET
- POST
- PUT
- DELETE
-
What status code is expected when a resource is successfully created?
- 200 OK
- 201 Created
- 404 Not Found
- 500 Internal Server Error
-
What type of request would you use to update an existing resource?
- GET
- POST
- PUT
- DELETE
-
Which method should you use to remove a resource from the server?
- GET
- POST
- PUT
- DELETE
With this knowledge, you’re now ready to begin testing APIs in Postman and understand the primary HTTP methods in CRUD operations.