NSjonas
NSjonas

Reputation: 12081

Azure API management with URL Template Parameters and Function Backend

I'm trying to setup an endpoint on Azure API Management with url parameters that routes to an Azure function.

EG: GET my-api-gateway.azure-api.net.com/product/{productId}

My inbound policies just set the backend and move the template param to a query param.

    <inbound>
        <base />
        <set-query-parameter name="productId" exists-action="override">
            <value>@(context.Request.MatchedParameters["productId"])</value>
        </set-query-parameter>
        <set-backend-service id="apim-generated-policy" backend-id="product-function" />
    </inbound>

However, when I call my-api-gateway.azure-api.net.com/product/123, the /123 also gets passed to the backend function url

https://my-function.azurewebsites.net/api/product-function/123?productId=123

which results in a 404.

Is there someway to make this work?

Upvotes: 3

Views: 7151

Answers (2)

YK1
YK1

Reputation: 7622

One way to resolve is to add route to the backend as you have shown in your own answer. However, if you cannot do that, then answer to your original question is to introduce a uri rewrite:

<inbound>
    <base />
    <rewrite-uri template="/product-function" copy-unmatched-params="false" />
    <set-query-parameter name="productId" exists-action="override">
        <value>@(context.Request.MatchedParameters["productId"])</value>
    </set-query-parameter>
    <set-backend-service id="apim-generated-policy" backend-id="product-function" />
</inbound>

Upvotes: 2

NSjonas
NSjonas

Reputation: 12081

The issue was that I also needed to setup routes my the function itself.

In my function.json, I added the following to match my API and now it works:

 "route": "product-function/{productId?}"

Upvotes: 1

Related Questions