Reputation: 1061
I have a single backend defined, for this example named myserver
with a runtime url https://myserver.com
. Also, I have "Authorization credentials" set for this backend:
The header x-custom-header
is added with a secret value. Every request needs to provide this header.
My backend provides the following endpoints:
GET https://myserver.com/Devices
GET https://myserver.com/Devices/device-1
POST https://myserver.com/Devices
Get https://myserver.com/Weather?location=Vienna
...
Here comes the problem: I want to provide multiple APIs specific for the data:
Devices
:
https://mycompany-apim.azure-api.net/Devices
Weather
:
https://mycompany-apim.azure-api.net/Weather
For some reason, I don't understand how I can achieve that with only a single backend without too much configuration hassle. I have configured <set-backend-service backend-id="myserver" />
but that obviously does not correctly resolve https://mycompany-apim.azure-api.net/Devices
to GET https://myserver.com/Devices
but only to GET https://myserver.com/
.
Some solutions that might work:
myserver-devices
with the base url https://myserver.com/Devices
rewrite-uri
policy for each endpoint in each API.
Are there alternatives?
Upvotes: 1
Views: 872
Reputation: 3937
This can be achieved by creating one API e.g. 73219639
and the required operations:
It's important that each APIM operation has the same path e.g. /Devices
:
https://rfqapiservicey27itmeb4cf7q.azure-api.net/Devices -> https://myserver.com/Devices
This is the simple working yaml:
openapi: 3.0.1
info:
title: '73219639'
description: ''
version: '1.0'
servers:
- url: 'https://rfqapiservicey27itmeb4cf7q.azure-api.net'
paths:
/Devices:
post:
summary: Devices
description: Devices
operationId: post-devices
responses:
'200':
description: OK
get:
summary: Devices
description: Devices
operationId: get-devices
responses:
'200':
description: OK
/Weather:
get:
summary: Weather
description: Weather
operationId: get-weather
responses:
'200':
description: OK
components:
securitySchemes:
apiKeyHeader:
type: apiKey
name: Ocp-Apim-Subscription-Key
in: header
apiKeyQuery:
type: apiKey
name: subscription-key
in: query
security:
- apiKeyHeader: []
- apiKeyQuery: []
Configure the Backend in the All operations
policy with an existing backend
:
<policies>
<inbound>
<base />
<set-backend-service backend-id="myserver-com" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
Do not change/modify any other policy. It's working with the default ones.
GET Devices - Test:
HTTP request
GET https://rfqapiservicey27itmeb4cf7q.azure-api.net/Devices HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
Ocp-Apim-Trace: true
Trace: set-backend-service (18.701 ms)
{
"message": "Backend service URL was changed.",
"oldBackendServiceUrl": "https://rfqapiservicey27itmeb4cf7q.azure-api.net/",
"newBackendServiceUrl": "https://myserver.com/",
"request": {
"url": "https://myserver.com/Devices"
}
}
Trace: forward-request (0.127 ms)
{
"message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
"request": {
"method": "GET",
"url": "https://myserver.com/Devices",
"headers": [
{
"name": "Host",
"value": "myserver.com"
},
{
Upvotes: 1