Jepzen
Jepzen

Reputation: 3192

Read Named values from the context in Azure Apim

I'm defining a inbound policy and trying to construct a URL from 2 components. I know how to get these values 1 but 1 but when I have to combine them into one cant figure out the syntax.

First I call a api and save the response

<send-request mode="copy" response-variable-name="createdUser" timeout="20" ignore-error="false">
        <set-url>{{Systems.One.Endpoint}}/services/rest</set-url>
</send-request> 

I can trace my response is ok like this

<trace source="Contact Id">@(((IResponse)context.Variables["createdUser"]).Body.As<JObject>()["contactId"])</trace>

But I dont know how to create a url from the {{System.One.Endpoint}} and the @(((IResponse)context.Variables["createdUser"]).Body.As()["contactId"])

This here does not work

<send-request mode="copy" response-variable-name="createPeriod" timeout="seconds" ignore-error="true">
 <set-url>
        {{System.Two.Endpoint}}/api/@(((IResponse)context.Variables["createPeriod"]).Body.As<JObject>()["contactId"])/createperiod/{{DefaultPeriodlength}}
</set-url>

Everything after the @ is just being set as string and not loaded from the actual value form the variable

How can I combine these two values?

Upvotes: 2

Views: 3818

Answers (1)

Vitaliy Kurokhtin
Vitaliy Kurokhtin

Reputation: 7810

Policy expressions can only be used to produce whole value for element/attribute. Named values in contract can be used to represent any part of any attribute/element value, and are inserted into policy prior to it being analyzed/executed.So something like this:

<set-url>@("{{System.Two.Endpoint}}/api/" + ((IResponse)context.Variables["createPeriod"]).Body.As<JObject>()["contactId"].ToString() + "/createperiod/{{DefaultPeriodlength}}")</set-url>

or if you prefer string interpolation:

<set-url>@($"{{System.Two.Endpoint}}/api/{((IResponse)context.Variables["createPeriod"]).Body.As<JObject>()["contactId"]}/createperiod/{{DefaultPeriodlength}}")</set-url>

Upvotes: 3

Related Questions