azure apim策略调用并打印json中的值

4si2a6ki  于 2023-02-16  发布在  其他
关注(0)|答案(1)|浏览(139)

我正面临着挑战,从json的身体打印价值在azure apim政策

{    "value": [        {            "counterKey": "abc123",            "periodKey": "0_86400",            "periodStartTime": "2023-02-10T00:00:00Z",            "periodEndTime": "2023-02-11T00:00:00Z",            "value": {                "callsCount": 4,                "kbTransferred": 0.36328125            }        }    ],    "count": 1,    "nextLink": null}

我想打印“callsCount”:4、通过API政策

<send-request ignore-error="true" timeout="20" response-variable-name="apref" mode="new">
        <set-url>myurl</set-url>
        <set-method>GET</set-method>
        <set-header name="Authorization" exists-action="override">
            <value>@("Bearer " + (string)context.Variables["PSItoken"])</value>
        </set-header>
    </send-request>
    <set-header name="NumberOfCounts" exists-action="override">
        <value>@{
           var jsonBody = ((IResponse)context.Variables["apref"]).Body.As<JObject>()["value"].ToString();
           JArray jBody = JArray.Parse(jsonBody); 
           return String.Join("", jBody.Select(i => i.ToString())); 
          
        }</value>
    </set-header>
    <set-variable name="NumberOfCounts" value="@(context.Request.Headers.GetValueOrDefault("NumberOfCounts"))" />

我的保险单到底漏了什么?

pprl5pva

pprl5pva1#

每当打印API响应时,使用响应变量名并添加返回响应策略:

<return-response response-variable-name="apref">
        <set-status code="200" reason="OK" />
        <set-header name="Content-Type" exists-action="override">
        <value>application/json</value></set-header>
        <set-body>@{
           var jsonBody = ((IResponse)context.Variables["apref"]).Body.As<JObject>()["value"].ToString();
           JArray jBody = JArray.Parse(jsonBody); 
          return String.Join("", jBody.Select(i => i.ToString())); 
              }
        </set-body>
     </return-response>

相关问题