[英]How to send JSON object as JSON String with Postman?
I want to send a JSON request but problem is I need to send my userPropertiesAsJsonString field as JSON string.我想发送 JSON 请求,但问题是我需要将 userPropertiesAsJsonString 字段作为 JSON 字符串发送。
How can I send userPropertiesAsJsonString as JSON string?如何将 userPropertiesAsJsonString 作为 JSON 字符串发送?
{
"User" : {
"userId" : "11111",
"userPropertiesAsJsonString" : ?
}
}
userPropertiesAsJsonString is; userPropertiesAsJsonString 是;
{
"properties" : {
"propertyName" : "test",
"propertyDesc" : "desc"
}
}
Try this :试试这个:
{
"User" : {
"userId" : "11111",
"userPropertiesAsJsonString" : "{\"properties\" : {\"propertyName\" : \"test\",\"propertyDesc\" : \"desc\"}}"
}
}
pre-request script:预请求脚本:
let query = {}
pm.environment.set('query', JSON.stringify(query));
body:身体:
{{query}}
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. Jason Mullings的回答对我不起作用,但它是一个很好的基础,使我能够为与您的问题非常相似的问题提出解决方案。
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 .将
userPropertiesAsJsonString
变量字符串化两次将允许您转义 JSON 字符串(从此答案中获得的解决方案;请参阅此要点以获得更详细的解释),然后您将获得一个请求正文,该正文看起来像由萨那萨蒂安。
Similar to other answers but:与其他答案类似,但:
pm.variables.set('myJsonAsEscapedString',JSON.stringify(`{"user_id": 1, "name": "Jhon"}`))
That way you can use it without escaping explicitly.这样你就可以在没有 escaping 的情况下使用它。
As JSON means JavaScript Object Notation, so you can just copy the userPropertiesAsJsonString into the original JSON:由于 JSON 表示 JavaScript Object Notation,因此您只需将 userPropertiesAsJsonString 复制到原始 JSON 中即可:
{
"User" : {
"userId" : "11111",
"userPropertiesAsJsonString" : {
"properties" : {
"propertyName" : "test",
"propertyDesc" : "desc"
}
}
}
}
Copy and paste this JSON into the Postman request body (raw formatted) and set the header "Content-Type: application/json".将此 JSON 复制并粘贴到 Postman 请求正文(原始格式)中,并设置标题“Content-Type: application/json”。
If you have to do more fancy stuff before the request you can execute a pre-request script in Postman: https://www.getpostman.com/docs/postman/scripts/pre_request_scripts如果您必须在请求之前做更多花哨的事情,您可以在 Postman 中执行预请求脚本: https : //www.getpostman.com/docs/postman/scripts/pre_request_scripts
For more about JSON see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON有关 JSON 的更多信息,请参见此处: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.