I want to send a JSON request but problem is I need to send my userPropertiesAsJsonString field as JSON string.
How can I send userPropertiesAsJsonString as JSON string?
{
"User" : {
"userId" : "11111",
"userPropertiesAsJsonString" : ?
}
}
userPropertiesAsJsonString is;
{
"properties" : {
"propertyName" : "test",
"propertyDesc" : "desc"
}
}
Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.
Postman builds and stores this variable for use between portions of the application, and may execute code and build things in an order or manner that you are not expecting. Postman does its own serialization of the data, since we cannot know what you intend to do with your data. By using JSON. stringify() and JSON.
As JSON means JavaScript Object Notation, so you can just copy the userPropertiesAsJsonString into the original JSON: Copy and paste this JSON into the Postman request body (raw formatted) and set the header "Content-Type: application/json".
Since the request sent from PostMan doesn't contain content type for JSON form data parameter, Jersey read it as String rather than a JSON object. The request content from post man is: `Cache-Control: no-cache. Postman-Token: 5241a928-78e9-2b70-637a-96a68775c85b.
When I examined the data in the Postman Console, it appeared that they were being stored in a JSON object, yet I was unable to access the values correctly on subsequent requests. For viewers of my Postman Blindfold Challenge, we all learned that I love to peel back the layers on what’s really going on inside a system.
The json part of the body should also be set as "File" rather then "Text", and put your json data in a json file for example "a.json". Just find out that this method doesn't work on windows, but works fine on linux.
Jason Mullings' answer did not work for me, but it was an excellent base that allowed me to come up with a solution to a problem very similar to yours.
In the Pre-request Script tab,
const userPropertiesAsJsonString = {
"properties" : {
"propertyName" : "test",
"propertyDesc" : "desc"
}
}
pm.environment.set(
'userPropertiesAsJsonString',
JSON.stringify(JSON.stringify(userPropertiesAsJsonString))
);
Then, in the Body tab,
{
"User" : {
"userId" : "11111",
"userPropertiesAsJsonString" : {{userPropertiesAsJsonString}}
}
}
Stringifying the userPropertiesAsJsonString
variable twice will allow you to escape the JSON string (solution obtained from this answer; refer to this gist for a more detailed explanation) which will then allow you to obtain a request body that looks like the one in the answer provided by sanatsathyan.
Try this :
{
"User" : {
"userId" : "11111",
"userPropertiesAsJsonString" : "{\"properties\" : {\"propertyName\" : \"test\",\"propertyDesc\" : \"desc\"}}"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With