如何在 Azure api 管理中使用 set-body 编辑 json 负载
How to edit a json payload with set-body in azure api management
我想以这种方式使用 set-body 编辑出站负载:
{"link": "http://localhost:7071/api"}
到
{"link":""} <- 其他一些 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
的值来生成新的 body。尝试:
JObject inBody = context.Response.Body.As<JObject>();
inBody["link"] = "https://randomlink/269";
return inBody.ToString();
我想以这种方式使用 set-body 编辑出站负载:
{"link": "http://localhost:7071/api"} 到 {"link":""} <- 其他一些 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
的值来生成新的 body。尝试:
JObject inBody = context.Response.Body.As<JObject>();
inBody["link"] = "https://randomlink/269";
return inBody.ToString();