英文:
Liquid body inside the return response policy
问题
请问有人知道在使用<return response>策略时,如何从<set-body template="liquid">语句内部访问请求主体?当我像这样做时,主体似乎是空的:
期望的结果应该是:
结果是:
英文:
Does anyone know how to access the request body from inside the <set-body template = "liquid"> statement when using the <return response> policy?
When I do like this the body seem to be empty:
<inbound>
<base />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<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>
The request body is:
{"firstname" : "John Doe"}`
Expected result would be:
{"name":"John Doe"}
The result is:
{"name":""}
答案1
得分: 2
Microsoft 支持告诉我,策略 return-response
不支持在 liquid 模板中使用 set-body
。
您可以通过在在 return-response
中使用之前修改 body 来实现一个变通方法。
入站策略:
<inbound>
<base />
<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>
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
string body = context.Request.Body.As<string>(true);
return body;
}</set-body>
</return-response>
</inbound>
请求:
POST https://rfqapiservicey27itmeb4cf7q.azure-api.net/sample/liquid HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
Content-Type: application/json
{"firstname" : "John Doe"}
响应:
HTTP/1.1 200 OK
content-length: 35
content-type: application/json
date: Mon, 09 Jan 2023 04:21:51 GMT
request-context: appId=cid-v1:a10dc7c9-c354-40a2-acf3-1401681f7808
vary: Origin
{
"name": "John Doe"
}
英文:
Microsoft support told me that the policy return-response
does not support set-body
with liquid template.
You can do a workaround by modifying the body before using it in return-response
.
Inbound policy:
<inbound>
<base />
<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>
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
string body = context.Request.Body.As<string>(true);
return body;
}</set-body>
</return-response>
</inbound>
Request:
POST https://rfqapiservicey27itmeb4cf7q.azure-api.net/sample/liquid HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
Content-Type: application/json
{"firstname" : "John Doe"}
Response:
HTTP/1.1 200 OK
content-length: 35
content-type: application/json
date: Mon, 09 Jan 2023 04:21:51 GMT
request-context: appId=cid-v1:a10dc7c9-c354-40a2-acf3-1401681f7808
vary: Origin
{
"name": "John Doe"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论