[英]Azure API Management Policy(how to do rewrite-uri)
I am new to Azure API Management.我是 Azure API 管理的新手。 I am constructing a policy that will change the initial request url.How can I extract the insurer_id from body and pass it to my backend.我正在构建一个将更改初始请求 url 的策略。如何从正文中提取insurer_id 并将其传递给我的后端。
{
"insurer_name": "Tony",
"insurer_id": "12345",
"comments": "This is test",
}
mybackend.com/api/relationship/approve/{insurer_id} mybackend.com/api/relationship/approve/{insurer_id}
I know I have to extract the id as JObject, store it in the variable and do a rewrite-uri.我知道我必须将 id 提取为 JObject,将其存储在变量中并执行 rewrite-uri。 But I am not sure, how I can go about implementing this in the Azure Policy.但我不确定,我如何在 Azure 政策中实现这一点。 A small example would be helpful.一个小例子会很有帮助。
Thank you谢谢
You have to read the insurer_id
from request body and set it to a variable .您必须从请求正文中读取insurer_id
并将其设置为变量。
<set-variable name="insurerId" value="@{
var body = context.Request.Body.As<JObject>(true);
return body["insurer_id"].Value<string>();
}" />
This variable can be used in rewrite-uri :此变量可用于rewrite-uri :
<rewrite-uri template="@("/api/relationship/approve/" + context.Variables.GetValueOrDefault<string>("insurerId") copy-unmatched-params="false" />
The complete policy:完整的政策:
<policies>
<inbound>
<base />
<set-variable name="insurerId" value="@{
var body = context.Request.Body.As<JObject>(true);
return body["insurer_id"].Value<string>();
}" />
<rewrite-uri template="@("/api/relationship/approve/" + context.Variables.GetValueOrDefault<string>("insurerId") copy-unmatched-params="false" />
<set-backend-service base-url="https://mybackend.com" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.