Steps to Retrieve the Request Object
Step 1 :
- Set up a request in a collection, including URL, parameters, query strings, headers, and body data.
- In the URL “localhost:3000”, we’ve added query parameters “company=gfg” and “country=india” to filter data by company and country. Query parameters are extra information in the URL for interaction between client and server, commonly used for filtering, sorting, and pagination.
- Additionally, we’ve added a path variable “id=10” to fetch data of the document with ID 10. Path variables help identify specific resources, making APIs more flexible with parameterized endpoints.
Step 2:
- The “Bearer token” added in the headers serves for authentication and authorization purposes, particularly beneficial in distributed systems and microservices architectures.
- Authentication: The token holds information about a specific valid logged-in user, verifying their identity.
- Authorization: Additionally, the token can include details regarding permissions, allowing or restricting access to certain resources based on user privileges.
Step 3:
- In the image, we’ve included a role with the value “admin” in the body of the request. This mechanism is employed for transmitting form data or additional information to the server.
- Furthermore, it can facilitate file uploads and the transmission of intricate data that isn’t suitable for query parameters, headers, or path variables.
Step 4:
- Navigate to either “Pre-request Script” or “Tests” section, depending on whether you want to inspect the request before sending it or after receiving the response.
- For the “Pre-request Script” section, you can add the following line of code to log the request object before it’s sent:
Step 5:
- Initiate the request by clicking on the blue-colored button named “Send”.
- View the output by clicking on the “Console” button located at the bottom left corner of the Postman window. Alternatively, you can access it through the main menu by selecting “View” > “Show Postman Console”.
Step 6:
In the Postman Console, you’ll find the logged pm.request
object. Click on the object to expand and examine its properties, including URL, method, headers, and body information. This object serves as a valuable tool for debugging and comprehending the structure of our requests. It provides access to the following:
pm.request.url
: Contains URL information such as the path, query parameters, and more.pm.request.method
: Specifies the HTTP method of the request (GET, POST, etc.).pm.request.headers
: Presents a collection of the request headers.pm.request.body
: Offers information about the request body, particularly useful for POST, PUT, and similar requests.
Step 7:
- We can see that apart from host, port, variable and query params ,we get a lot of other information like Content-Type, Date, etc.
- The data that we are sending can be seen in the above image whether it is query params, body , path variables or headers.
Note: While modifications to the request object in Pre-request Script or Tests do not change the request sent by Postman, they are essential for setting up variables, preparing the environment, or testing assertions on the response. To dynamically alter the request itself, such as modifying headers or the body, variables or scripts must be used before the request runs.
How to Retrieve the Request Object in PostMan
Postman is a popular API testing tool used by developers to test, document, and share APIs. While Postman primarily focuses on sending HTTP requests and receiving responses, it also provides features for inspecting request objects. In this article, we’ll explore how to retrieve the request object in Postman and use it for testing and debugging purposes.