What Are The Methods Used In Postman
Understanding the Methods Used in Postman for API Testing
Postman is a powerful tool for API testing, offering a wide range of methods to interact with APIs and verify their functionality. This guide explores the common methods used in Postman, providing practical examples and step-by-step instructions to help you get started.
1. GET Method
The GET
method is used to retrieve data from a specific resource. It’s idempotent, meaning multiple identical requests have the same effect as a single request.
Example: Fetching a list of users from a REST API
- Create a New Request: Open Postman and create a new request.
- Set the Method: Select
GET
from the dropdown menu. - Enter the Request URL: In the request URL field, type the endpoint for fetching users, for example,
https://api.example.com/users
. - Send Request: Click the
Send
button.
Sample Code:
GET https://api.example.com/users
// Expected Response (JSON format)[ { "id": 1, "name": "John Doe", "email": "john.doe@example.com" }, { "id": 2, "name": "Jane Doe", "email": "jane.doe@example.com" }]
2. POST Method
The POST
method is used to create new resources on a server. It’s not idempotent, as multiple identical requests will result in multiple resources being created.
Example: Creating a new user account
- Create a New Request: Open Postman and create a new request.
- Set the Method: Select
POST
from the dropdown menu. - Enter the Request URL: In the request URL field, type the endpoint for creating new users, for example,
https://api.example.com/users
. - Set the Body: In the
Body
tab, chooseraw
and select the appropriate content type (e.g.,application/json
). Provide the user data in JSON format:
{ "name": "New User", "email": "newuser@example.com"}
- Send Request: Click the
Send
button.
Sample Code:
POST https://api.example.com/users
// Request Body (JSON format){ "name": "New User", "email": "newuser@example.com"}
// Expected Response (JSON format){ "id": 3, "name": "New User", "email": "newuser@example.com"}
3. PUT Method
The PUT
method is used to update an existing resource. It’s idempotent, meaning multiple identical requests will have the same effect as a single request.
Example: Updating an existing user’s name
- Create a New Request: Open Postman and create a new request.
- Set the Method: Select
PUT
from the dropdown menu. - Enter the Request URL: In the request URL field, type the endpoint for updating users, for example,
https://api.example.com/users/1
. - Set the Body: In the
Body
tab, chooseraw
and select the appropriate content type (e.g.,application/json
). Provide the updated user data in JSON format:
{ "name": "Updated User"}
- Send Request: Click the
Send
button.
Sample Code:
PUT https://api.example.com/users/1
// Request Body (JSON format){ "name": "Updated User"}
// Expected Response (JSON format){ "id": 1, "name": "Updated User", "email": "john.doe@example.com"}
4. DELETE Method
The DELETE
method is used to delete a resource from the server. It’s idempotent, meaning multiple identical requests will have the same effect as a single request.
Example: Deleting a user account
- Create a New Request: Open Postman and create a new request.
- Set the Method: Select
DELETE
from the dropdown menu. - Enter the Request URL: In the request URL field, type the endpoint for deleting users, for example,
https://api.example.com/users/1
. - Send Request: Click the
Send
button.
Sample Code:
DELETE https://api.example.com/users/1
// Expected Response (Empty Body or Status Code 204)
5. PATCH Method
The PATCH
method is used to partially update a resource. It’s not idempotent, as multiple identical requests can result in different resource states.
Example: Updating a user’s email address
- Create a New Request: Open Postman and create a new request.
- Set the Method: Select
PATCH
from the dropdown menu. - Enter the Request URL: In the request URL field, type the endpoint for patching users, for example,
https://api.example.com/users/1
. - Set the Body: In the
Body
tab, chooseraw
and select the appropriate content type (e.g.,application/json
). Provide the updated data in JSON format:
{ "email": "updated.email@example.com"}
- Send Request: Click the
Send
button.
Sample Code:
PATCH https://api.example.com/users/1
// Request Body (JSON format){ "email": "updated.email@example.com"}
// Expected Response (JSON format){ "id": 1, "name": "John Doe", "email": "updated.email@example.com"}
6. Using Postman Methods in Tests
These methods can be used within Postman’s testing features to create robust tests for your APIs. You use assertions to check the response against expected values and ensure that the API behaves correctly.
Example: Testing a POST request:
- Create a Test Script: After sending the POST request, click the “Tests” tab in Postman.
- Add Tests: Write code in the test script to verify the response:
pm.test("Status code is 201", function () { pm.response.to.have.status(201);});
pm.test("Response has a valid user ID", function () { pm.expect(pm.response.json().id).to.be.a('number');});
pm.test("Response has the expected user name", function () { pm.expect(pm.response.json().name).to.equal("New User");});
7. Choosing the Right Method
The choice of method depends on the specific API operation you want to perform. Use GET
for retrieving data, POST
for creating new resources, PUT
for updating entire resources, PATCH
for partial updates, and DELETE
for removing resources.
By understanding the different methods used in Postman and incorporating them into your test scenarios, you can ensure the reliability and quality of your APIs.