液体体内返回响应策略

huangapple go评论58阅读模式
英文:

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 &lt;set-body template = "liquid"> statement when using the &lt;return response> policy?
When I do like this the body seem to be empty:

&lt;inbound&gt;
    &lt;base /&gt;
    &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
        &lt;value&gt;application/json&lt;/value&gt;
    &lt;/set-header&gt;
    &lt;return-response&gt;
        &lt;set-status code=&quot;200&quot; reason=&quot;OK&quot; /&gt;
        &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
            &lt;value&gt;application/json&lt;/value&gt;
        &lt;/set-header&gt;
        &lt;set-body template=&quot;liquid&quot;&gt;
            {
            &quot;name&quot;:&quot;{{body.firstname}}&quot;
            }
            
        &lt;/set-body&gt;
    &lt;/return-response&gt;
&lt;/inbound&gt;

The request body is:

{&quot;firstname&quot; : &quot;John Doe&quot;}`

Expected result would be:

{&quot;name&quot;:&quot;John Doe&quot;}

The result is:

{&quot;name&quot;:&quot;&quot;}

答案1

得分: 2

Microsoft 支持告诉我,策略 return-response 不支持在 liquid 模板中使用 set-body

您可以通过在在 return-response 中使用之前修改 body 来实现一个变通方法。

入站策略:

&lt;inbound&gt;
    &lt;base /&gt;
    &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
        &lt;value&gt;application/json&lt;/value&gt;
    &lt;/set-header&gt;
    &lt;set-body template=&quot;liquid&quot;&gt;
{
   &quot;name&quot;:&quot;{{body.firstname}}&quot;
}
    &lt;/set-body&gt;
    &lt;return-response&gt;
        &lt;set-status code=&quot;200&quot; reason=&quot;OK&quot; /&gt;
        &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
            &lt;value&gt;application/json&lt;/value&gt;
        &lt;/set-header&gt;
        &lt;set-body&gt;@{   
            string body = context.Request.Body.As&lt;string&gt;(true);
            return body;
        }&lt;/set-body&gt;
    &lt;/return-response&gt;
&lt;/inbound&gt;

请求:

POST https://rfqapiservicey27itmeb4cf7q.azure-api.net/sample/liquid HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
Content-Type: application/json

{&quot;firstname&quot; : &quot;John Doe&quot;}

响应:

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

{
    &quot;name&quot;: &quot;John Doe&quot;
}
英文:

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:

&lt;inbound&gt;
    &lt;base /&gt;
    &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
        &lt;value&gt;application/json&lt;/value&gt;
    &lt;/set-header&gt;
    &lt;set-body template=&quot;liquid&quot;&gt;
{
   &quot;name&quot;:&quot;{{body.firstname}}&quot;
}
    &lt;/set-body&gt;
    &lt;return-response&gt;
        &lt;set-status code=&quot;200&quot; reason=&quot;OK&quot; /&gt;
        &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
            &lt;value&gt;application/json&lt;/value&gt;
        &lt;/set-header&gt;
        &lt;set-body&gt;@{   
            string body = context.Request.Body.As&lt;string&gt;(true);
            return body;
        }&lt;/set-body&gt;
    &lt;/return-response&gt;
&lt;/inbound&gt;

Request:

POST https://rfqapiservicey27itmeb4cf7q.azure-api.net/sample/liquid HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
Content-Type: application/json

{&quot;firstname&quot; : &quot;John Doe&quot;}

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

{
    &quot;name&quot;: &quot;John Doe&quot;
}

液体体内返回响应策略

huangapple
  • 本文由 发表于 2023年1月9日 05:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定