Home > Net >  How to edit a json payload with set-body in azure api management
How to edit a json payload with set-body in azure api management

Time:09-21

I want to edit an outbound payload using set-body in this way:

{"link": "http://localhost:7071/api"} to {"link":""} <- some other link

I have tried this but there is no change in outbound:

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; 

CodePudding user response:

To explain why your code does not work:

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

You never actually change value of str to produce new body. Try:

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