【问题标题】:How to edit a json payload with set-body in azure api management如何在 azure api 管理中使用 set-body 编辑 json 有效负载
【发布时间】:2021-09-20 06:27:24
【问题描述】:

我想以这种方式使用 set-body 编辑出站负载:

{“链接”:“http://localhost:7071/api”} 到 {"link":""}

我试过了,但出站没有变化:

JObject inBody = context.Response.Body.As<JObject>(); 
string str = inBody.ToString();
var item = JObject.Parse("{ 'link': 'http://localhost:7071/api}");
item["link"] = "https://randomlink/269";
return str; 

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: azure azure-api-management


【解决方案1】:

解释为什么你的代码不起作用:

JObject inBody = context.Response.Body.As<JObject>(); //Request
payload has been parsed and stored in `inBody` variable.
string str = inBody.ToString(); //`inBody` converted to string and stored in `str` variable.
var item = JObject.Parse("{ 'link': 'http://localhost:7071/api}"); //Some other JSON parsed and stored in `item` variable
item["link"] = "https://randomlink/269"; //`item` variable updated with new value
return str; //Returning `str` variable as new body value

你永远不会真正改变 str 的值来产生新的身体。试试:

JObject inBody = context.Response.Body.As<JObject>(); 
inBody["link"] = "https://randomlink/269";
return inBody.ToString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2019-01-23
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多