Open In App

Postman – How to Sent .json File as POST Data Payload within Pre-request Script?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is quite a popular tool that is primarily used for API testing. There are various features in this including sending different kinds of requests to the web servers and getting the responses in real time. In this article, we will be focusing on how to use the Postman to send .json files as POST send payload within a pre-request script.

Primary Terminologies

  1. API (Application Programming Interface): APIs are used to make a connection between different computer programs so they can share pieces of information.
  2. JSON (JavaScript Object Notation): A simple text format for storing and sharing data so that both people and computers can easily read and understand.
  3. Pre-request Script: It’s a set of instructions that runs before sending a request in Postman, it is used to prepare or set up certain things.

How to Send .json file as POST Data Payload Within Pre-Request Script?

Step 1: Setting up the Postman

Download and install the Postman. Create a collection and inside that create a request.

Screenshot-2023-12-20-174838-(1)

Creating a request after creating the GeeksforGeeks Collection.

Step 2: Writing the Pre-request Script

Navigate to the ‘Pre-request Script’ tab in your request window, here we can write the javascript code. Now here we will write the script, in this case, we are setting up an environment variable and there are two fields name and value with their respective values and we will use this environment variable in our POST request by referencing it by double curly braces.

pre

adding the Pre-request Script for our post-request.

Step 3: Configuring the Request

Now, here we have to configure the Request so that it can send the request properly, for that select the ‘Body’ Tab inside that among various options choose the raw option, and on the rightmost side from the dropdown choose the JSON format. Here we will insert the data and will use the data saved in the environment variable by referencing it with double curly braces. One more thing to note here, make sure to use the same name of the variable that is used in the pre-request script, otherwise we can get some error.

Screenshot-2024-01-14-040107

Using the environment variable, so that it can use the data from the pre-request script.

Step 4: Sending the Request

Now it’s the final part, here we will send the request, and for that enter the URL with the correct API endpoint. In this case, we are using a placeholder API for testing purposes, after that select the POST as the method and click on the send button.

send

Sending the Post request

Step 5: Verifying the Response

Check the Response: Once the request is sent, the response will be displayed in the lower pane of Postman. Here, we can see that the value we have set in the pre-request script is showing here, hence it is working fine.

Screenshot-2024-01-24-063416

Final Response

Conclusion

Sending a .json file as a POST data payload in a pre-request script in Postman is a powerful technique. It allows for dynamic and flexible API testing, especially in scenarios where the payload needs to be generated or altered programmatically. Mastering this technique can significantly enhance your API testing capabilities, making your testing process more robust and efficient.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads