使用<return response>策略时,如何在Liquid模板的<set-body>中获取请求体?
解决策略中Liquid模板无法访问请求体的问题
问题核心在于**body变量自然是空值。
要解决这个问题,只需在<read-body>策略,显式触发请求体的读取与解析操作,后续的Liquid模板就能正常访问body对象了。
修改后的完整配置如下:
<inbound> <base /> <set-header name="Content-Type" exists-action="override"> <value>application/json</value> </set-header> <!-- 添加read-body策略,强制读取并解析请求体 --> <read-body /> <return-response> <set-status code="200" reason="OK" /> <set-header name="Content-Type" exists-action="override"> <value>application/json</value> </set-header> <set-body template="liquid"> { "name":"{{body.firstname}}" } </set-body> </return-response> </inbound>
配置更新后,发送请求体{"firstname" : "John Doe"},就能得到预期的响应结果:
{"name":"John Doe"}
内容的提问来源于stack exchange,提问作者K.J.M.O.
相关产品推荐
相关产品推荐

