How to add a query suffix to url in API Manager without the q=

Shaun 50 Reputation points
2024-04-19T05:52:40.1433333+00:00

Hi

I have several API's set up in APIM running correctly for GET and Post requests. The GET requests return a data lists when querying the endpoint or a specific record when adding the /{id#} to the url.

I can run the Get endpoints fine, but the apim docs are not clear how to structure the policies to rewrite the url or query parameters to achive the required url format.

eg:

Get https/testapi.com/jobs - returns the jobs data
Get https/testapi.com/jobs/2342-dada-1232-dasd is required to return the specific job record

if i use the query parameter to pass the id in the url ends up like this:
Get https/testapi.com/jobs?q=/2342-dada-1232-dasd instead of the required structure above

please advise how to use the policies to effect the required url structure above

Thanks

Shaun

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,792 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,002 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Aki Nishikawa 800 Reputation points Microsoft Employee
    2024-04-19T16:09:32.2133333+00:00

    Have you checked the following policy snippets?

    https://github.com/Azure/api-management-policy-snippets/blob/master/policy-expressions/README.md

    These snippets might be helpful for you. If you can retrieve values from query/path parameters, you can construct a URLs with path/query parameters to align with backend service. Here are samples.

    [Base URL] APIM (API Service): https://<instance>.azure-api.net/api

    Backend Service: https://<BackendService_URL>/api

    [1] Query paramter to path parameter

    https://<instance>.azure-api.net/api/path/{param} -> https://<BackendService_URL>/api/query?q={param}

        <inbound>
            <base />
            <rewrite-uri id="setPathParam" template="@{ 
                    return "/query?q="+context.Request.MatchedParameters.GetValueOrDefault("param","notfound"); 
            }" />
        </inbound>
    

    [2] Path parameter to query parameter

    https://<instance>.azure-api.net/api/query?q={param} -> https://<BackendService_URL>/api/path/{param}

        <inbound>
            <base />
            <rewrite-uri id="setQueryParam" template="@{ 
                            return "/path/"+context.Request.Url.Query.GetValueOrDefault("q", "notfound"); 
                }" />
        </inbound>
    
    0 comments No comments

  2. Aki Nishikawa 800 Reputation points Microsoft Employee
    2024-04-22T03:36:57.42+00:00

    Another option to convert path parameter to query parameter is also available. If you define path parameter {param} in Azure API Management frontend, this option would be simpler.

    <policies>
        <inbound>
            <base />
            <rewrite-uri id="setQueryParam" template="/query?q={param}" />
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
        </outbound>
        <on-error>
            <base />
        </on-error>
    </policies>
    
    0 comments No comments