简体   繁体   中英

Azure API management - how to access product information

In APIM, I am trying to access the Product information at API level policy, as i have to execute some logic based on product name. I am using the below code

<policies>
    <inbound>
        <set-variable name="ProductName" value="@{
            return  context.Product.Name;
        }" />

But, when trying to post request from postman, i can see the below exception in trace.

{
source: "set-variable",
timestamp: "2020-08-19T14:42:24.4936554Z",
elapsed: "00:00:00.0358409",
data:- {
messages:- [
-{
message: "Expression evaluation failed.",
expression: " return context.Product.Name; ",
details: "Object reference not set to an instance of an object."
},
"Expression evaluation failed. Object reference not set to an instance of an object.",
"Object reference not set to an instance of an object."
]
}
}

Why it is null? is this the case, that i can't access the property in inbound scope. Need guidance. Or, is there any other way i can access the Product.Name property. Thank you.

You can get the Product Name with @(context.Product.Name) .

<inbound>
    <base />
    <set-variable name="aaa" value="@(context.Product.Name)" />
    <set-body template="liquid">
    {
        "success": true,
        "var1": {{context.Variables["aaa"]}}
    }
    </set-body>
</inbound>

In test, set the product name as Starter and you will get the snapshot as below.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM