Policy Fragment problem in API Management

asked 2022-06-02T05:40:12.513+00:00
Rourke, Fergus 21 Reputation points

I have an inbound policy in API Management that sends the request payload to an Azure Service Bus Queue.
I want to move the part of the policy that authenticates and sets the backend service to a policy fragment.

This works in my API policy
<set-backend-service base-url="@("https://{{serviceBusNamespace}}.servicebus.windows.net/" + (string)context.Variables["queue"] + "/messages?api-version=2015-01")" />

However it produces the following error when trying to save the exact same in a policy fragment.

The policy fragment is not a valid XML: 'https' is an unexpected token. Expecting white space. Line 18, position 36.

Are there any limitiations on what can go in to a policy fragment? I cant see anything here that would stop me doing that. https://learn.microsoft.com/en-us/azure/api-management/policy-fragments

EDIT: Seems to only be a problem when setting attributes.

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

Accepted answer
  1. answered 2022-06-06T08:54:37.093+00:00
    JananiRamesh-MSFT 9,286 Reputation points Microsoft Employee

    @Rourke, Fergus Thanks for reaching out. I tried the same inhouse and I was able to reproduce the issue. It seems to be an issue with Payload format: XML we need to change that to Payload format: rawxml there is no way to modify this in azure portal however, as a workaround you can use Postman to achieve this

    Please replace the below values appropriately

    https://management.azure.com/subscriptions/**{sub id}/resourceGroups/{Resource group name}/providers/Microsoft.ApiManagement/service/{Service Name}/policyFragments/{Fragment Name}**?api-version=2021-12-01-preview


    {"id":"/subscriptions/{sub id}/resourceGroups/{Resource group name}/providers/Microsoft.ApiManagement/service/{Service Name}/policyFragments/{Fragment Name}","type":"Microsoft.ApiManagement/service/policyFragments","name":"{Fragment Name}","properties":{"value":"<!--\n IMPORTANT:\n - Policy fragment are included as-is whenever they are referenced.\n - If using variables. Ensure they are setup before use.\n - Copy and paste your code here or simply start coding\n-->\n<fragment>\n <set-backend-service base-url=\"@(\"https://{ {serviceBusNamespace}}.servicebus.windows.net/\" + (string)context.Variables[\"queue\"] + \"/messages?api-version=2015-01\")\" />\n</fragment>\n","description":"","format":"rawxml"}}

    you will get 202 Accepted response. Later you will see the policy fragment created in the azure portal.

    We will create a work item to fix this bug, please try the above steps and let me know incase of further queries, I would be happy to assist you.

    To benefit the community find the right answers, please do mark the post which was helpful by clicking on Accept Answer’ & ‘Up-Vote’.

    No comments

1 additional answer

Sort by: Most helpful
  1. answered 2022-06-07T23:33:49.073+00:00
    Rourke, Fergus 21 Reputation points

    Thanks, that works and also it looks like the portal has been updated now too.

    No comments