javascript 如何使用 Postman 将 JSON 对象作为 JSON 字符串发送?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48317152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send JSON object as JSON String with Postman?
提问by hellzone
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"
}
}
回答by sanatsathyan
Try this :
试试这个 :
{
"User" : {
"userId" : "11111",
"userPropertiesAsJsonString" : "{\"properties\" : {\"propertyName\" : \"test\",\"propertyDesc\" : \"desc\"}}"
}
}
回答by Jason Mullings
pre-request script:
预请求脚本:
let query = {}
pm.environment.set('query', JSON.stringify(query));
body:
身体:
{{query}}
回答by dwettstein
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

