How to dynamically set headers name and header values after fetch from a JSON Array body?

Azure-search 201 Reputation points
2023-08-08T15:14:41.5+00:00

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>
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,368 questions
{count} votes

Accepted answer
  1. Pramod Valavala 20,656 Reputation points Microsoft Employee
    2023-08-08T15:25:21.5766667+00:00

    @Azure-search Unfortunately, there is no way to do this dynamically within APIM policies. Feel free to up vote this feature request for it to gain traction.

    The workaround would be to use a custom API that could perform this transformation for you. You could consider building a simple function app for example.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.