Azure API Management Policy - Dynamic named value select
Asked Answered
C

1

8

I have stored a named value based on the user id, so I can identify the payload submitter, with a reference of my choosing.

When I use the policy below, I get the value as a string, rather than actually looking up the named value. If I simply write the user id inside the double curly brackets rather than getting it dynamically from the context, the lookup works fine and it substitues with the named value I want.

Is it not possible to do in a simple manner?

<policies>
  <inbound>
    <set-header name="x-request-context-data" exists-action="override">
      <value>{{@(context.User.Id)}}</value>
    </set-header>
  </inbound>
</policies>

Output:

x-request-context-data: {{@(context.User.Id)}}
Coarsegrained answered 25/10, 2019 at 7:24 Comment(0)
C
7

Answered at MSDN.

While you can use named values in policy expressions, the other way around is not supported and not possible to support as described in this feature request.

Workarounds would be to either

  • Store named values as a JSON string or a CSV string with key=value pairs. Then use policy expressions to parse and fetch the required value.
  • Use control flow policies for run time decisions.
  • For complex cases, offload to a function app but use value caching policies for improved latency if possible
Coarsegrained answered 28/10, 2019 at 11:59 Comment(2)
Detailed solution on how to implement JSON String in Named Value pair is available here link and here linkDoelling
Another solution is to use Policy Fragment and store all key value pairs in variables, which can be directly used in Policy with variable reference without a hassle of any additional parsing. also Named Value pairs plain text value has character limit, so if you are planning to load lot of data in JSON format, it would lead to problem in future. Using Policy Fragment is better solution than using Named Value pairs.Doelling

© 2022 - 2024 — McMap. All rights reserved.