gpt4 book ai didi

Azure APIM 策略改变有效负载主体

转载 作者:行者123 更新时间:2023-12-03 03:35:11 33 4
gpt4 key购买 nike

我目前仍在 APIM 中探索。我在有效负载 #1 中有一个有效负载,我想将其转换为另一个有效负载 #2(预期)。
我将如何编辑我的策略来构建此有效负载?

有效负载1

{
"dependtee_name": "Steve",
"depdentee_last_name": "Rogers",
"dependtee_comment" : "This is test",
"file": "file.txt",
"file_type": "text file",
"insurer_name": "Steve",
"insurer_last_name": "Rogers",
"insurer_comment" : "This is test",
"extra_info": "This is comments"
}

有效负载 2 - 预期

{
"dependtee_info": {
"name": "Steve",
"last_name": "Rogers"

},

"file_details": {
"all_file": [ "file.txt", "null" ],
"fileFormat": "text file"
},

"insurer_info": {
"i_name": "Tony",
"i_last_name": "Stark"
},
"extra_info": "This is comments"
}

当前代码

<policies>
<inbound>
<base />
<return-response>
<set-status code="200" reason="ok" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var body = context.Request.Body.As<JObject>(true);

var transformedBody = new JObject();
transformedBody["extra_info"] = body["extra_info"];


var dependentee= new JObject();
dependetee["name"] = body["dependtee_name"];
dependentee["lastName"] = body["depdentee_last_name"];
transformedBody["dependtee_info"] = dependentee;



var file_details = new JObject();
dependentee["all_file"] = body["file"];
dependentee["fileFormat"] = body["file_type"];
transformedBody["file_details"] = file_details;

var insurer_info = new JObject();
dependetee["i_name"] = body["insurer_name"];
dependentee["i_last_name"] = body["insurer_last_name"];
transformedBody["insurer_info"] = insurer_info;

return transformedBody.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>

希望专门更改文件的正文。
我想知道如何在我的预期有效负载中添加这些方括号。
有效负载 2 应该有多个键“all_file”值,但我希望能够从有效负载 1 获取它。

感谢您的帮助

最佳答案

您必须创建一个 JArrayAdd字符串并将 JArray 分配给父对象:

var file_details = new JObject();
var all_File = new JArray();
all_File.Add(body["extra_info"]);
all_File.Add("null"); // does not makes sense to a null string
file_details["all_file"] = all_File;
file_details["fileFormat"] = body["file_type"];
transformedBody["file_details"] = file_details;

我不明白 Payload 2 - Expected"null" 的用途:
"all_file": [ "file.txt", "null"], 它从哪里来?为什么它是字符串 "null" 而不是 null

这是完整的固定策略:

<policies>
<inbound>
<base />
<return-response>
<set-status code="200" reason="ok" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var body = context.Request.Body.As<JObject>(true);

var transformedBody = new JObject();
transformedBody["extra_info"] = body["extra_info"];


var dependentee= new JObject();
dependentee["name"] = body["dependtee_name"];
dependentee["lastName"] = body["depdentee_last_name"];
transformedBody["dependtee_info"] = dependentee;

var file_details = new JObject();
var all_File = new JArray();
all_File.Add(body["extra_info"]);
all_File.Add("null"); // does not makes sense to use a null string
file_details["all_file"] = all_File;
file_details["fileFormat"] = body["file_type"];
transformedBody["file_details"] = file_details;

var insurer_info = new JObject();
insurer_info["i_name"] = body["insurer_name"];
insurer_info["i_last_name"] = body["insurer_last_name"];
transformedBody["insurer_info"] = insurer_info;

return transformedBody.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>

结果:
enter image description here

关于Azure APIM 策略改变有效负载主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73596821/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com