如何在azure api管理中使用set-body编辑json有效负载



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

{"link"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; 

解释为什么你的代码不能工作:

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();

最新更新