I am trying to use Azure Pipeline to build my API Management infrastructure automatically and have successfully added the API and API Operation but having trouble defining the Operation specific Policy.
I have this policy that I based on a very useful article https://www.serverlessnotes.com/docs/expose-service-bus-queue-through-api-management
<policies>
<inbound>
<base />
<set-variable name="sasToken" value="@{
return "bob";
}" />
<set-header name="Authorization" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<string>("sasToken"))</value>
</set-header>
<set-header name="Content-type" exists-action="override">
<value>application/json</value>
</set-header>
<set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" />
<set-header name="BrokerProperties" exists-action="override">
<value>@{
return string.Format("{{"SessionId":"{0}"}}", "bob");
}</value>
</set-header>
<set-backend-service base-url="https://i365intfnapidevtbcoresb.servicebus.windows.net/i365intfnapidevtbcoresbqueue" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
There is actually more code in the value bits, but for illustration I’ve removed them.
However if I put this in a separate file (or even an inline variable) and run the Azure Powershell command
Set-AzApiManagementPolicy -Context $apim_context -ApiId $apiId -OperationId addmessage -PolicyFilePath <path to policy xml file>
Where the $ values are variables I declared previously.
I get the error
Error Details: [Code= ValidationError, Message= 'bob' is an unexpected token. Expecting white space. Line 5, position 21., Target= representation]
Basically, I cant work out how to format the function thats in the value attribute or value elements. The bit starting with @{}.
I can enter the policy via the Azure API Management screen no problem, but cant do it via the Set-AzApiManagementPolicy command.
Any ideas on how to format it.
Thanks.
2
Answers
Solved it in the end, You have to encode some of the characters to make it work. For example @quot; instead of "
So I have in my Azure Powershell script:
and this now seems to do the trick.
You would probably also need to do the same for c# things like:
would be
to make it work otherwise it conflicts with the xml
Thanks mclayton for pointing me in the right direction.
It will escape it for you if you use the Format parameter.