setting multiple headers or variables in APIM

Nagashree Balasundaram 741 Reputation points
2022-06-29T16:45:49.147+00:00

Hi,
Is it possible to set multiple headers or variables in a single policy?
The reason I am asking is, there are instances where you parse a Json payload and try to set different headers with values from different attributes defined in the json payload.
Currently, the only way I have been able to do this is to use multiple set-header policies and in each one, i am having to parse the json payload and set the value. If all the required headers can be set or at least required values extracted into multiple variables it would save parsing the json payload multiple times and writing the same lines of code multiple times.
Example below : to populate accesstoken and expiry variables below - the json parsing is being done twice
<set-variable name="varBody" value="@(context.Response.Body.As<string>())" />
<set-variable name="accesstoken" value="@{
JObject json = JObject.Parse(context.Variables.GetValueOrDefault<string>("varBody"));
var accesstoken = (string)json.GetValue("access_token");
return accesstoken;
}" />
<set-variable name="expiry" value="@{
JObject json = JObject.Parse(context.Variables.GetValueOrDefault<string>("varBody"));
var expiry = (int)json.GetValue("expires_in");
return expiry;
}" />

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,166 questions
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 27,831 Reputation points
    2022-07-07T18:33:09.35+00:00

    @Nagashree Balasundaram Thanks for reaching out. Yes, you can set multiple headers and variables in a single policy. The policy looks good but instead of parsing the JObject multiple times you can refine your policy as below

    218689-image.png

    please let me know incase of further queries, I would be happy to assist you.

    To benefit the community find the right answers, please do mark the post which was helpful by clicking on Accept Answer’ & ‘Up-Vote’.

    1 person found this answer helpful.

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.