【问题标题】:Fetch value from json request body and store in a variable in Azure API Management从 json 请求正文中获取值并存储在 Azure API 管理中的变量中
【发布时间】:2021-03-10 14:31:29
【问题描述】:

我想从 json 中获取一个值并将其存储在 Azure API 管理中的一个变量中。 请求正文中的 JSON 示例是

{
    "ItemCode": 1,
    "ItemName": "USA",
    "typeCode": "REG"
  }

我需要获取 ItemCode 和 typeCode 的值并将其存储在变量中。

我检查了 Microsoft Docs,它让我可以使用液体模板来转换身体,我认为这对我的要求没有用处。

我已经将 JSON 存储在一个变量中,例如

set-variable name="varItemCode" value="@(context.Request.Body.As<String>(preserveContent:true))" />

由于这是一个字符串,我无法遍历 JSON 对象。

【问题讨论】:

    标签: azure azure-api-management


    【解决方案1】:

    我做到了

    <kbd>set-variable name="varTypeCode" value="@{
                        JObject json = JObject.Parse(context.Variables.GetValueOrDefault<string>("varBody"));
                        var typeCode = json.GetValue("typeCode");
                        return typeCode;
                     }" />
    

    【讨论】:

      【解决方案2】:

      这是有效的

       <set-variable name="validationResults" value="@(context.Request.Body.As<JObject>())" />
          
      <set-variable name="operation" value="@((string)((JObject)context.Variables["validationResults"])["operation"])" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-05
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-25
        相关资源
        最近更新 更多