Can someone please help me out on this as i have been stuck on this code for days, and there isn't much in documentation regarding this.
I have below JSON array:
"keys": [
{
"key": "header1,
"value": "testabc"
},
{
"key": "header2",
"value": "testdef"
}
]
I have saved this body inside a variables called "keyarray" . I need to set the header name and header value which is the value of "key" and "value" (mentioned in the body).
It should set the number of headers according to all the values present for "key" and "value"
Eg:
Header 1-> Header name: "header1"(value of "key") and Header Value: "testabc" (value of "value")
Header 2-> Header name: "header2"(value of "key") and Header Value: "testdef"(value of "value")
I tried the below solutions, but none of them seem to work
Solution 1:
<for-each item="JObject" in="@(context.Variables["keyarray"] as JArray)">
<set-header name="@((string)item["key"])" exists-action="override">
<value>@((string)item["value"])</value>
</set-header>
</for-each>
Issue: for-each is not a valid policy in azure apim
Solution 2:
<set-header name="@{
var jsonarray = JObject.Parse((String)context.Variables.GetValueOrDefault<JObject>("keyarray"));
foreach (var k in new [] {"key"}) {
jsonarray.GetValue(k);
}
return jsonarray.ToString();
}" exists-action="override">
<value>@{
var jsonarray = JObject.Parse((String)context.Variables.GetValueOrDefault<JObject>("keyarray"));
foreach (var v in new [] {"value"}) {
jsonarray.GetValue(v);
}
return jsonarray.ToString();
}"</value>
</set-header>