Azure APIM "rewrite-uri" policy - How to remove the URL suffix?
Asked Answered
M

1

10

I have a requirement where I have the Azure APIM public facing URL - https://api.example.com/api/v1/storenumber/ordernumber

And

Back-end service URL (Notice - There is no URL path suffix here) - https://back-end.service.com

So I need to remove "/api/v1/storenumber/ordernumber" in APIM.

How to achieve this in Azure APIM policies?

Thanks, Aneesh.

Mchugh answered 22/1, 2019 at 18:52 Comment(0)
T
16

Let's say you have APIM service with hostname my-service.azure-api.net. An API in APIM service with URL suffix my-api, and a backend URI of https://my-backend.com. And an operation in this API with URI template of my/uri/template.

So for an incoming call to

https://my-service.azure-api.net/my-api/my/uri/template

without any additional configuration APIM service will make a call to

https://my-backend.com/my/uri/template

Because by default APIM service replaces in source URI scheme+hostname+api suffix with backend URI defined for an API in question.

In most simple case when your API has only one operation you could set API suffix to api/v1/storenumber/ordernumber and operation template to /. That would result in public facing URI of https://api.example.com/api/v1/storenumber/ordernumber

and backend URI for this operation of

https://back-end.service.com/

Of course this is approach is harder to use when you have multiple operations in API. In that case you'll need to use policies: https://learn.microsoft.com/en-us/azure/api-management/api-management-transformation-policies#RewriteURL. For that you can set your API suffix and operation URI template to anything, but add this policy into operation's inbound section:

<rewrite-uri template="/" />

what rewrite-uri policy does is overrides operation URI template for backend request.

Tellus answered 24/1, 2019 at 17:56 Comment(2)
Works, but leaves a single / at the end. Is there a way to remove that one as well?Durwyn
I found a workaround for the issue with the /. Simply replace this with a ? not sure if there is a more elegant solution but this does the trick.Pastelist

© 2022 - 2024 — McMap. All rights reserved.