David Klempfner
David Klempfner

Reputation: 9870

How do I add in bound processing policy in APIM?

I am trying to add the following in bound processing policy to my APIM for an operation:

<policies>
    <inbound>
        <base />
        <rewrite-uri template="/stores/{Location}/slots?StartDate={StartDateTime}&amp;AppointmentType={AppointmentType}" />
        <set-header name="ocp-apim-subscription-key" exists-action="override">
            <value>12d0bdd57ca84fa9ad35f13f22605dbf</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

I have found this commandlet, however there is no information on what to put in -Policy.

I have tried using the following

    $policyString = '<policies>
    <inbound>
        <base />
        <rewrite-uri template="/stores/{Location}/slots?StartDate={StartDateTime}&amp;AppointmentType={AppointmentType}" />
        <set-header name="ocp-apim-subscription-key" exists-action="override">
            <value>12d0bdd57ca84fa9ad35f13f22605dbf</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>'

Set-AzureRmApiManagementPolicy -Context $apiMgmtContext -ApiId $apiId -Policy $policyString -OperationId 'GetCalendar'

But it gives me this error:

Operation returned an invalid status code 'BadRequest'

What format is the Policy param expecting?

Upvotes: 1

Views: 506

Answers (2)

David Klempfner
David Klempfner

Reputation: 9870

Thanks to Joy Wang for pointing me in the right direction, here's the solution:

I tried adding the XML directly through the portal, and got this error:

One or more fields contain incorrect values: Error in element 'rewrite-uri' on line 4, column 10: Only parameters specified in the original URL template can be used in the rewrite template

Turns out the parameters I was referring to (StartDateTime and AppointmentType) were not part of the template params in the APIM.

Once I added them as template params in APIM, the command worked.

Would be good if the Powershell commandlet returned the same error as the Azure Portal, rather than just 'BadRequest'

Upvotes: 0

Joy Wang
Joy Wang

Reputation: 42043

I can reproduce your issue, the format of -Policy parameter seems to be correct, the issue was caused by the rewrite-uri in your policy.

<rewrite-uri template="/stores/{Location}/slots?StartDate={StartDateTime}&amp;AppointmentType={AppointmentType}" />

enter image description here

I test it with the sample in the official doc, it works fine.

<rewrite-uri template="/put" />

enter image description here

For more details about the usage of rewrite-uri, you could refer to this link.

Upvotes: 1

Related Questions