How To Send Http Request Using Postman
A Comprehensive Guide to Sending HTTP Requests Using Postman for API Testing
Postman is a powerful tool that allows you to test APIs by sending HTTP requests and inspecting the responses. This guide will provide you with a step-by-step walkthrough of how to send various types of HTTP requests using Postman, along with practical examples and sample codes to help you get started.
Sending an HTTP Request: A Basic Example
- Open Postman: Launch the Postman application or access the web version.
- Create a New Request: Click on the “New” button in the top-left corner to create a new request.
- Choose the Request Method: Select the appropriate HTTP method from the dropdown menu, such as GET, POST, PUT, DELETE, etc.
- Enter the Request URL: In the “Enter request URL” field, type the endpoint URL of the API you want to interact with.
- Add Headers: Optional: If the API requires specific headers, such as authorization tokens or content types, click on the “Headers” tab and add them as key-value pairs.
- Send the Request: Click the “Send” button to send the request to the API server.
- Inspect the Response: The response from the API will be displayed in the “Body” tab, including the status code, headers, and response content.
Sample Code:
// Example GET request to retrieve data from a public API// Request URL: https://reqres.in/api/usersGET https://reqres.in/api/users
Sending a POST Request with a JSON Body
- Choose the POST Method: Select “POST” from the HTTP method dropdown.
- Set the Content Type Header: In the “Headers” tab, add a header with the key “Content-Type” and the value “application/json”.
- Enter the Request Body: In the “Body” tab, select the “raw” option and choose “JSON” as the format.
- Enter JSON Data: Write your JSON payload in the editor.
Sample Code:
// Example POST request to create a new user// Request URL: https://reqres.in/api/usersPOST https://reqres.in/api/usersContent-Type: application/json
{ "name": "John Doe", "job": "QA Engineer"}
How to send an HTTP Request using Postman with Parameters
- Add Parameters to the URL: Append query parameters to the end of your request URL using the format “?key1=value1&key2=value2”.
- Add Parameters in the Body: For POST, PUT, or PATCH requests, you can include parameters within the request body using JSON format.
Sample Code:
// Example GET request with query parameters// Request URL: https://reqres.in/api/users?page=2GET https://reqres.in/api/users?page=2
Understanding HTTP Response Codes
- 200 OK: Request successful.
- 201 Created: Resource created successfully.
- 400 Bad Request: The request was invalid.
- 401 Unauthorized: The request was not authorized.
- 404 Not Found: The resource was not found.
- 500 Internal Server Error: An error occurred on the server.
Sending Multiple Requests: Collections and Environments
Collections:
- Organize related requests into logical groups.
- Share and collaborate on API tests with your team.
- Use variables to reuse values across requests.
Environments:
- Store global variables like API keys, base URLs, and environment-specific settings.
- Easily switch between different environments (e.g., development, testing, production).
Testing with Assertions: Validating API Responses
Postman allows you to write tests using Javascript to validate API responses. This enables you to:
- Verify the status code of the response.
- Assert the presence and values of specific fields in the response.
- Ensure the response meets your expected format.
Sample Code:
// Test script for a successful response and checking the value of "name"pm.test("Status code is 200", function () { pm.response.to.have.status(200);});
pm.test("Name should be 'John Doe'", function () { pm.expect(pm.response.json().name).to.eql("John Doe");});
How to Send HTTP Request using Postman with Authorization
Many APIs require authentication to access resources. Postman provides different authorization methods:
- Basic Auth: Simple authentication using username and password.
- Bearer Token: Authenticate using a JWT or other access token.
- OAuth 2.0: A more complex authentication protocol commonly used for web applications.
- API Key: Authenticate using a unique API key provided by the service.
Sample Code:
// Example using Bearer Token authorization// Request URL: https://example.com/api/protected-resourceGET https://example.com/api/protected-resourceAuthorization: Bearer your_access_token
Debugging and Troubleshooting
- Use the console tab to view any errors or messages from your test scripts.
- Enable request logging to capture the complete details of each request and response.
- Utilize the Postman debugger to step through your test scripts and examine their execution.
By learning how to send HTTP requests effectively using Postman and understanding the basics of API testing, you can streamline your testing process, ensure the quality of your APIs, and confidently deploy your applications.