How to Cache POST request?

How to Cache the POST request?

The Cache for GET request worked using Response Cache Policy but not for POST.

Need to cache POST request, no need to hit the resource if the POST request is same by one of the element in the payload.

0 8 2,556
8 REPLIES 8

Im not aware of a reason for the response cache to not work with a POST request. What kind of response does your backend generate?

However with that said, implementing this behaviour may deviate from the HTTP RFC. See RFC 2616, 13.10

@dane knezic

Thank you for the reply.

I mean to say cache works only if the uri changes and not when the payload changes. So I needed to cache based on one of the element in payload , if that element varies then it should not cache instead call the backend service.

@Nesara D M,

By default apigee response cache policy caches post requests also.pls refer to the trace session and sample code here here trace-1545942655304.txt

responsecacheexample-rev1-2018-12-27.zip

Pls refer to the below post on the don'ts for response cache policy.

https://community.apigee.com/articles/19671/using-the-responsecache-policy-responsibly-or-dont.html

Thank you for the reply.

I mean to say cache works only if the uri changes and not when the payload changes. So I needed to cache based on one of the element in payload , if that element varies then it should not cache instead call the backend service.

@Nesara D M ,

Welcome to Apigee Community 🙂

You can cache API requests independent of HTTP verb and Apigee provides all the granularity you need to control the cache behaviour.

Please find 4 Minute Videos that explains Apigee Response Caching in action, https://www.youtube.com/playlist?list=PLsWqc60hQz4cP2SOZkMM9Gwri25uICZzj

You can also find detailed documentation in Apigee Docs.

Having said that, Why do you want to cache POST request ? By design, POST is for CREATE Entity and response is dynamic. You might need to fundamentally go back to API Design.

-------------------------------

Anil Sagar

5997-screen-shot-2017-11-23-at-75916-pm.png Learn Apigee Concepts in 4 Minutes HandsOn

@Anil Sagar @ Google

Thank you for the reply.

I understand POST should not be used but I have a requirement where I get the common response when the payload has some unique value in it.

Please find below how I need the POST to get executed.

I mean to say cache works only if the uri changes and not when the payload changes. So I needed to cache based on one of the element in payload , if that element varies then it should not cache instead call the backend service.

Before implementing you may want to consider if the requirement follows HTTP/API/Restful best practices and standards. It sounds like you may be on the path to creating a counter intuitive API, and it may not work as expected by your users.

sidd-harth
Participant V

Hi @Nesara D M, as suggested by other posts, using cache for POST method is like a REST Anti-Pattern and it should be avoided. Frankly speaking, this is just a standard and not mandatory. So you can go ahead and apply cache to POST methods, but take care to document this properly so that the App Developers are aware of this.

I mean to say cache works only if the uri changes and not when the payload changes. So I needed to cache based on one of the element in payload , if that element varies then it should not cache instead call the backend service.

Use an ExtractVariable policy to get the element(JSON/XML/etc) into a variable. Next, use a Cache Policy and use the extracted variable in KeyFragment.

For example, I am considering JSON payload,

<ExtractVariables name="ExtractVariables-3">
   <Source>response</Source>
   <JSONPayload>
      <Variable name="latitude" type="float">
         <JSONPath>$.results[0].geometry.location.lat</JSONPath>
      </Variable>
   </JSONPayload>
   <VariablePrefix>geocoderesponse</VariablePrefix>
</ExtractVariables>
<ResponseCache name="ResponseCache">
    <CacheKey>
        <KeyFragment ref="geocoderesponse.latitude" />
    </CacheKey>
    <ExpirySettings>
        <TimeoutInSec>600</TimeoutInSec>
    </ExpirySettings>
</ResponseCache>

In the above code, I have used only one KeyFragment, but we can add more than one KeyFragment based on requirement.