1

I have a requirement to add trailing slash to the APIs hosted in APIM. However, APIM gateway seems to be removing the trailing slash when forward the call to backend service.

Example 1:

APIM Gateway URL: https://api.domain.com/getdata/

Backend service URL: https://mybackendservice.domain.com/getdata

Example 2:

APIM Gateway URL: https://api.domain.com/getdata/?q=1&r=2

Backend service URL: https://mybackendservice.domain.com/getdata?q=1&r=2

Can someone please suggest any URL rewrite policies we can add to retain the trailing slash, so that the backend service urls would be:

https://mybackendservice.domain.com/getdata/

https://mybackendservice.domain.com/getdata/?q=1&r=2

James Z
  • 12,209
  • 10
  • 24
  • 44
Karim
  • 11
  • 1

1 Answers1

0

Thank you Vitaliy Kurokhtin ,Posting your suggestion as an answer to help other community members.

"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: Rewrite URL |MS DOC . 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"

Reference: Azure APIM "rewrite-uri" policy - How to remove the URL suffix?

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15