简体   繁体   中英

Azure API policy JSON conversion while returning the response from cache

I am using below policy code and storing the data in the cache. While storing as string, it is working fine but requirement is to store in JSON format and its failing and throwing error during conversion.

<policies>
    <inbound>
        <base />
        <set-variable name="cacheKey" value="@(context.Request.MatchedParameters["flightno"])" />
        <cache-lookup-value key="@((string)context.Variables["cacheKey"])" variable-name="cachedResponseValue" />
        <set-variable name="FinalResponse" value="@((JObject)context.Variables["cachedResponseValue"])" />
        <choose>
            <when condition="@(context.Variables.ContainsKey("cachedResponseValue"))">
                <!-- If found in cache then get it from the cache and retun -->
                <return-response>
                    <set-body>@((JObject)context.Variables["FinalResponse"])</set-body>
                </return-response>
            </when>
        </choose>    
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <set-variable name="responseValue" value="@(context.Response.Body.As<JObject>(preserveContent: true))" />
        <!-- Store result in cache -->
        <cache-store-value key="@("" + context.Variables["cacheKey"])" value="@((JObject)context.Variables["responseValue"])" duration="30" />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

I'm able to store the payload as json in the cache, while retrieving,I want to return json and trying to parse it. Error am getting while parsing the response in tag for below syntax as

<set-body>@((JObject)context.Variables["FinalResponse"])</set-body>


Error in element 'set-body' on line 22, column 22: Expression return type 'Newtonsoft.Json.Linq.JObject' is not allowed.

It is working fine, if response data type kept as string like

 <set-body>@((string)context.Variables["FinalResponse"])</set-body>

Do you know how to convert the response body to json?

That is not allowed since set-body policy only accepts string or byte array content. Simple solution is to convert JObject to string:

<return-response>
    <set-header name="Content-Type">
        <value>application/json</value>
    </set-header>
    <set-body>@(((JObject)context.Variables["FinalResponse"]).ToString())</set-body>
</return-response>

Response will still be a valid JSON and with. set-header above does not affect how body is processed, but you need it in response to make client understand you're passing JSON.

The only thing here is that:

  1. Backend is called and response is parsed as JObject
  2. JObject is stored into cache, will be serialized
  3. JObject is read from cache, will be deserialized
  4. JObject is put into response, will be serialized as string

A lot of time is spent serializing and deserializing response with no benefit. If other parts of your policy do not need to work on response as JObject, a simpler and faster approach would be to simplu store it as a string.

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