如何解决从 set-body 中的 if 语句访问 MatchedParameters
在 REST API 的 APIM 策略中,我正在尝试为后端服务创建 SOAP 请求。 REST 请求的 URI 是 /GetCustomer/{accountNumber}。 在下面的 Liquid if 语句中,我试图检查 MatchedParameters 集合是否包含帐号。这不起作用,我从 else 分支获取值。 我尝试了几种变体:
- {% 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>
如果不使用 if 语句可以正常工作,那么请求就会按照我的预期构造:
<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>
所以问题是:是否可以从 Liquid if 语句中访问上下文?如果是这样,正确的语法是什么?
解决方法
我在我身边测试,你只需要像这样写液体:
{% if context.Request.MatchedParameters["accountNumber"] != null %}
{% else %}
{% endif %}
顺便说一句:根据我的测试,如果您在 APIM 的请求 url 中指定了 {accountNumber}。我们必须用 ...../GetCustomer/{accountNumber}
来请求它,如果我们用 ...../GetCustomer
来请求它,它会显示 404 错误。因此,您无需担心“如果 context.Request.MatchedParameters["accountNumber"] 存在”
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。