Azure Api Management url rewrite for dynamic path

Omar Amalfi 1 Reputation point
2021-10-25T07:17:15.957+00:00

Hi I'm having problems rewriting an Url path.

My Apim looks like: https://sample-apim-int.azure-api.net/download/

The backend: https://samplebackend.net/

I want to be able to redirect from: https://sample-apim-int.azure-api.net/download/[dynamic_content]?id=1

to: https://samplebackend.net/[dynamic_content]?id=1

dynamic_content could be any of: images, pdfs, docs ... and it is dynamic because users from backoffice could add or remove.

I have try this with no luck:
frontend
resource: /{path}

and inbound policy:

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,319 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,856 Reputation points
    2021-10-26T04:04:39.103+00:00

    @Omar Amalfi Your policy looks good and I have tested the same at my end and couldn't observe any issue.
    I am sharing my open API spec that I have used for testing

    {  
        "openapi": "3.0.1",  
        "info": {  
            "title": "Download",  
            "description": "",  
            "version": "1.0"  
        },  
        "servers": [{  
            "url": "https://{yourapiminstancename}.azure-api.net/download"  
        }],  
        "paths": {  
            "/{dynamic_content}": {  
                "get": {  
                    "summary": "dynamic_content",  
                    "operationId": "dynamic-content",  
                    "parameters": [{  
                        "name": "dynamic_content",  
                        "in": "path",  
                        "required": true,  
                        "schema": {  
                            "type": ""  
                        }  
                    }, {  
                        "name": "id",  
                        "in": "query",  
                        "schema": {  
                            "type": ""  
                        }  
                    }],  
                    "responses": {  
                        "200": {  
                            "description": null  
                        }  
                    }  
                }  
            }  
        },  
        "components": {  
            "securitySchemes": {  
                "apiKeyHeader": {  
                    "type": "apiKey",  
                    "name": "Ocp-Apim-Subscription-Key",  
                    "in": "header"  
                },  
                "apiKeyQuery": {  
                    "type": "apiKey",  
                    "name": "subscription-key",  
                    "in": "query"  
                }  
            }  
        },  
        "security": [{  
            "apiKeyHeader": []  
        }, {  
            "apiKeyQuery": []  
        }]  
    }  
    

    Policy:

    <policies>  
        <inbound>  
            <base />  
            <set-backend-service base-url="https://google.com/" />  
            <rewrite-uri template="/{dynamic_content}" copy-unmatched-params="true" />  
        </inbound>  
        <backend>  
            <base />  
        </backend>  
        <outbound>  
            <base />  
        </outbound>  
        <on-error>  
            <base />  
        </on-error>  
    </policies>  
    

    While testing from the portal I have passed the dynamic content and query parameter

    143547-image.png

    I have used the backend URL to be failed (returns 404 error) so the Ocp-Apim-Trace is generated and we can see what backend URL it has called.
    143610-image.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.