APIM Policy. Fetching {id} from URL path. RRS feed

  • Question

  • Hi

    I have just created my first test API in Azure APIM and have a path defined in my Frontend swagger:



    description: Henter en Leiekontrakt
    operationId: Finn-en-leiekontrakt
    summary: leiekontrakt
    - name: id
    in: path
    description: 'Intern identifikator for ...
    required: true
    type: string


    In inbound policy section I want to extract the value of {id} and pass it to backend service using f.i set-query-parameter policy. Something like an @expression with context.Request.Url.Split('/').Last() ...

    - How can I extract this value and pass it to backend service in a proper way?

    Thank you :-)


    Wednesday, September 5, 2018 2:12 PM


All replies

  • Try to use API Management Context Request MatchedParameters. “MatchedParameters” collection captures (and only captures) parameters that were present in operation UrlTemplate. Those are path and/or query parameters, but only those query parameters that are in template. For more information, you may refer to this document: https://blogs.msdn.microsoft.com/david_burgs_blog/2018/05/17/what-is-api-management-context-request-matchedparameters/.

    Reference: https://stackoverflow.com/questions/42255094/work-with-url-template-parameter-values-in-policy-templates.  


    If this answer was helpful, click “Mark as Answer” or Up-Vote. To provide additional feedback on your forum experience, click here
    • Proposed as answer by Swikruti Bose Thursday, September 6, 2018 4:56 AM
    • Marked as answer by Stig R Karlsen Thursday, September 6, 2018 9:48 AM
    Thursday, September 6, 2018 4:56 AM
  • Hi Bose

    Thank you for your answer. This was very helpful!

    My service does now work with the following policy setting

            <base />
            <!-- set context variable from frontend query or path -->
            <set-variable name="id" value="@(context.Request.MatchedParameters["id"])" />
                <when condition="@( (string)context.Variables["id"] != "")">
                    <!-- get context variable and append in backend query -->
                    <set-query-parameter name="Kontraktsnummer" exists-action="append">
            <rewrite-uri template="HentLeiekontrakter" />

    Using context.Request.MatchedParameters is better than my solution with context.Request.Utl.Path.Split('/').Last() beacuse it would handle an {id} other places than at the end of the url path.


    Stig Karlsen

    Thursday, September 6, 2018 9:47 AM
  • Glad to know the issue is resolved. Thanks for the update.
    Thursday, September 6, 2018 11:56 AM