为什么Set-Body策略无法识别我的POST正文? [英] Why does Set-Body policy not recognize my POST body?

查看:105
本文介绍了为什么Set-Body策略无法识别我的POST正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Azure(API管理)中的API终结点连接到后端服务.但是,set-body策略无法识别我的JSON主体,因此不会将其转换为后端调用.

I'm trying to connect an API endpoint in Azure (api management) to a backend service. However, the set-body policy isn't recognizing my JSON body and thus isn't transforming it for the backend call.

我已经尝试过所有可以想到的液体"和无"模板的迭代.微软文档是无用的,因为即使文档中的"liquid"模板也要大写,而它需要小写.甚至每个人都指向的Deep Dive文章都具有误导性和/或已过时.

I've tried all iterations i can think of for the "Liquid" and "None" templates. The microsoft documentation is useless as even the "liquid" template is capitalized in the doc while it NEEDS to be lowercase. Even the Deep Dive article that everyone points to is misleading and/or out of date.

我曾经能够使用液体模板获得{{context.Request.OriginalUrl}}引用来工作,但是我似乎无法获得{{body.json}}引用来工作

i was once able to get the {{context.Request.OriginalUrl}} reference to work using liquid template, but i can't seem to get the {{body.json}} reference to work

这是我在本节中制定的政策(完全可以测试-这对我在做什么没有用):

Here's the policy i have in the section (purely to test - this has no use for what i'm doing):

<set-body template="liquid">
    Calling User Agent: {{context.Request.OriginalUrl}}
</set-body>

这是我必须尝试读取json正文(通过POST传递)的示例:

And here's an example of what i have to try to read the json body (passing through via POST):

<set-body template="liquid">{{body}}</set-body>

我尝试了几次迭代和输入,如下所示:

I've tried several iterations and inputs like below:

<set-body template="liquid">{{body.json}}</set-body>

穿过这样的身体时:

{"json":"this is an example body"}

无论我做什么,这都是我在测试呼叫后在跟踪中看到的内容:

No matter what I do, this is what I see in the trace after testing the call:

set-body (0.069 ms)
{
    "input": null,
    "output": ""
} 

我显然愿意使用无"模板,但是我遇到了同样的问题.文档是错误的-如果我复制/粘贴示例:

i'm obviously open to using the "none" template, but i run into the same problems. The documentation is wrong - if i copy/paste the example:

<set-body>@(context.Body.As<String>())</set-body>

我收到如下错误:

One or more fields contain incorrect values:
Error in element 'set-body' on line 32, column 10: 'IProxyRequestContext' does not contain a definition for 'Body' and no extension method 'Body' accepting a first argument of type 'IProxyRequestContext' could be found (are you missing a using directive or an assembly reference?)

当我确实得到它没有错误时,它将返回相同的输出":"输出.

and when i do get it to not error, it returns the same "output":"" output.

推荐答案

为能够在液体模板中作为对象访问主体,您必须将Content-Type标头设置为application/json,如

For being able to access the body as an object in the liquid template, you will have to set the Content-Type header to application/json as mentioned in the docs.
If your requests are already sending this header, then it should work without setting it too.

inbound部分中的此类政策将保证其按预期运行

A policy like this in the inbound section will guarantee it works as expected

<set-header name="Content-Type" exists-action="override">
    <value>application/json</value>
</set-header>
<set-body template="liquid">{{body.json}}"}</set-body>

对于通过context变量进行访问,您必须按照

As for accessing it via the context variable, you have to access it as context.Request.Body.As<string>() as mentioned in the docs, so something like this

<set-body>@(context.Request.Body.As<string>())</set-body>

<<的官方参考c7> 似乎没有您提到的问题.
您要参考其他文档吗?如果它位于docs.microsoft.com上,则可以在每个文档的末尾打开一个问题.

The official reference for set-body doesn't seem to have the problems you've mentioned.
Was there any other doc you are referring to? If its on docs.microsoft.com, you could open an issue at the end of each doc.

这篇关于为什么Set-Body策略无法识别我的POST正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆