I want to test my server via sending raw json data. One of the values of my request body is a really big object, so it would be very helpful if I can read it from a json file instead of pasting a really big raw json string. Is it possible?
I don’ want to read the entire request body from a json file. Instead, I just want to read for one of the values. Thanks.
{
"key1": "value1", // a normal value
"key2": {...} // a very long object. I want to read this from file
}
here in the body I just need to replace the email field alone. So I have added curly braces and just named it as email. The name should be given to the attribute in the file as well. You don’t need to define anything more here. Postman is smart enough to read the variables names and match them with the input file provided.
Here I would just need to replace my email alone, and password remains the same.
In both the cases “email” is mandatory in the file because that’s the field you are referring to in your body.
So in your case,
{
"key1": "value1", // a normal value
"key2": "{{key2}}" // a very long object. I want to read this from file
}
you can access the key2 from the JSON file. If the field name is different in the JSON file, pass the field name from which you need to copy the value and pass it under key2.
Hope this is useful
Let me know if you have any other questions.
Hello @bpricilla , thank you so much for your reply . So this is really helpful but I am a bit confused. I need to the email field in the request get its content from an entire json file which is not starting with an array ([]).
What I have tried is in the runner, I read the file as data but I gave the error “json file should be an array”. Okay, I have converted it to an array which has one object in it. Then in the pre-request script, I tried to get the data with pm.environment.set("data", data[0]);. Then in the body, I tried to pass it via
"data": {{data}}
But it didn’t helped, when I run the runner it shows that data becomes null in the request body.
@emrehidden Okay, can you please share the screenshots if possible?
also if converting to a array is bit tough for you every time, you can opt for a csv file as well. For Data driven approach Postman supports, both CSV and JSON and the process is same
Also when the field/attribute name is same as your declaration inside the body you don’t need to specifically declare it as a environment/collection/global variable. You can directly read the name in your script.
As @pranavdavar mentioned you can use the variable name, inside double quotes to be in JSON format.
"data": "{{data}}"
Please try this and let us know know if you are facing issues still. Kindly share the screenshots to understand better