Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

azure api management - Accessing MatchedParameters from an if statement in set-body

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:

  1. {% if {{context.Request.MatchedParameters.ContainsKey("accountNumber")}} %}
  2. {% if @(context.Request.MatchedParameters.ContainsKey("accountNumber")) %}
  3. {% 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I test it in my side, you just need to write the liquid like:

{% if context.Request.MatchedParameters["accountNumber"] != null %}
    
{% else %}
    
{% endif %}

And by the way: According to my test, if you specify a {accountNumber} in the request url of APIM. We have to requsest it with ...../GetCustomer/{accountNumber}, if we request it with ...../GetCustomer, it will show 404 error. So you do not need to worry about "if context.Request.MatchedParameters["accountNumber"] is exist"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...