'set backend url after extracting it from header azure apim

Hi I am trying to implement such a functionality where i need to make sure that the api is going through gateway so have created a apim and i need to extract header from the call and route to that call : so far i have done this policy:

<policies>
    <inbound>
        <set-variable name="host" value="@(context.Request.Headers.GetValueOrDefault("uri","http://google.com/"))" />
        <set-backend-service base-url="${{ variables.host}}" />
        <base />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

but its giving error :

Error in element 'set-backend-service' on line 16, column 4: Value is not a valid absolute URL.


Solution 1:[1]

To resolve this issue, there are two things to do:

  1. Define the parameter type you are using in the template file.

  2. When defining param's you need to signify the start and end of the command using square brackets.

Try this:

<set-backend-service base-url=\"[parameters('backend_url')]\"/>

Refer this document to know about parameters in ARM templates.

Solution 2:[2]

<set-backend-service base-url="@((string)context.Variables["host"])" />

Solution 3:[3]

Error in element 'set-backend-service' on line 16, column 4: Value is not a valid absolute URL.

This probably means that the value of your uri header does not include https://. A valid absolute URL contains all the information necessary to locate a resource, including the protocol (HTTPS).

You can also set the value directly in the set-backend-service without using the set-variable first. Like:

<set-backend-service base-url="@(context.Request.Headers.GetValueOrDefault("header-name","optional-default-value"))" />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RajkumarMamidiChettu-MT
Solution 2 TempoClick
Solution 3 Gerben