In an APIM policy for a REST API, I'm trying to create a SOAP request for the backend service.
The URI of the REST request is /GetCustomer/{accountNumber}.
In the Liquid if statement below, I'm trying to check if the MatchedParameters collection contains the account number. This doesn't work, I get the value from the else branch.
I tried a couple of variations:
- {% if {{context.Request.MatchedParameters.ContainsKey("accountNumber")}} %}
- {% if @(context.Request.MatchedParameters.ContainsKey("accountNumber")) %}
- {% if context.Request.MatchedParameters.ContainsKey("accountNumber") %}
<set-body template="liquid">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://my-services.com/CustomerService/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetCustomer>
<request>
{% if {{context.Request.MatchedParameters.ContainsKey("accountNumber")}} %}
<AccountNumber>{{context.Request.MatchedParameters["accountNumber"]}}</AccountNumber>
{% else %}
<AccountNumber xsi:nil="true" />
{% endif %}
</request>
</GetCustomer>
</soap:Body>
</soap:Envelope>
</set-body>
Leaving the if statement out works correctly, then the request is constructed as I expect:
<set-body template="liquid">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://my-services.com/CustomerService/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetCustomer>
<request>
<AccountNumber>{{context.Request.MatchedParameters["accountNumber"]}}</AccountNumber>
</request>
</GetCustomer>
</soap:Body>
</soap:Envelope>
</set-body>
So the question is: is it possible to access the context from within a Liquid if statement? And if so, what is the correct syntax?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…