Title: Cache http post method
Last modified: October 24, 2022

---

# Cache http post method

 *  Resolved [emanuelx](https://wordpress.org/support/users/emanuelx/)
 * (@emanuelx)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/cache-http-post-method/)
 * Hi, the plugin works very well but only for GET requests.
 * There is a way to cache the http POST requests? A filter or something like that?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [Richard Korthuis](https://wordpress.org/support/users/rockfire/)
 * (@rockfire)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/cache-http-post-method/#post-16131894)
 * Hi [@emanuelx](https://wordpress.org/support/users/emanuelx/)
 * Thank you for using our plugin!
 * Yes there is a filter for that:
 *     ```
       /**
        * Allow POST requests to be cached.
        *
        * @param array $allowed_request_methods An array of request methods that are allowed to be cached.
        *
        * @return array
        */
       function wprc_filter_allowed_request_methods( $allowed_request_methods ) {
       	$allowed_request_methods[] = 'POST';
   
       	return $allowed_request_methods;
       }
   
       add_filter( 'wp_rest_cache/allowed_request_methods', 'wprc_filter_allowed_request_methods', 10, 1 );
       ```
   
 *  Thread Starter [emanuelx](https://wordpress.org/support/users/emanuelx/)
 * (@emanuelx)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/cache-http-post-method/#post-16131911)
 * Hi [@rockfire](https://wordpress.org/support/users/rockfire/) ,
 * Hi already added this filter on my code.
 *     ```
       add_filter( 'wp_rest_cache/allowed_request_methods', function () {
           return array('GET', 'POST');
       } );
       ```
   
 * > [View post on imgur.com](https://imgur.com/a/usQw7Gp)
 * But this causes another issue, the end endpoint is always the same but the content
   of HTTP body changes everytime and the plugin doesn’t save that information on
   databse.`
 * Do you have other filter to save also the content from body?
 *  Plugin Author [Richard Korthuis](https://wordpress.org/support/users/rockfire/)
 * (@rockfire)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/cache-http-post-method/#post-16131927)
 * Hi [@emanuelx](https://wordpress.org/support/users/emanuelx/)
 * So if I understand you correctly the response from the REST API changes with 
   each request? If that is correct than that is not a call you want to be cached.
   Caching is for responses that remain the same.
 *  Thread Starter [emanuelx](https://wordpress.org/support/users/emanuelx/)
 * (@emanuelx)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/cache-http-post-method/#post-16132273)
 * I don’t explain very well, but the endpoint is always the same but the parameters
   on body can be different.
 * Example:
    Request number 1 curl -X POST -H “Content-Type: application/json” -
   d ‘{“slug”: “hello-world”}’ [https://example/api/v1/customwordpress/post/read](https://example/api/v1/customwordpress/post/read)
 * Request number 2
    curl -X POST -H “Content-Type: application/json” -d ‘{“slug”:“
   test-1”}’ [https://example/api/v1/customwordpress/post/read](https://example/api/v1/customwordpress/post/read)
 * On my tests this doesn’t work well, because wp rest cache doesn’t look at body.
 *  Plugin Author [Richard Korthuis](https://wordpress.org/support/users/rockfire/)
 * (@rockfire)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/cache-http-post-method/#post-16138742)
 * Hi [@emanuelx](https://wordpress.org/support/users/emanuelx/)
 * So you would want the cache to differentiate based on the POST body? That is 
   not something we have implemented because a POST request is used to create or
   update a resource and that isn’t something you would usually cache.
 * Could you explain your use case a little bit further, so we can understand why
   you would want it cached? That way we can determine if we can find a workable
   solution for you.
 *  Thread Starter [emanuelx](https://wordpress.org/support/users/emanuelx/)
 * (@emanuelx)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/cache-http-post-method/#post-16149641)
 * Hi [@rockfire](https://wordpress.org/support/users/rockfire/),
 * Basically, I don’t use GET methods in my endpoints, because GET method isn’t 
   too save as POST method.
 * So, if you can find a way to cache the POST requests based on POST body will 
   be great to my application.
 *  Plugin Author [Richard Korthuis](https://wordpress.org/support/users/rockfire/)
 * (@rockfire)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/cache-http-post-method/#post-16152839)
 * Hi [@emanuelx](https://wordpress.org/support/users/emanuelx/)
 * I have discussed your issue internally. Since in general POST requests should
   not be cached we are reluctant to do so. Furthermore differentiating caches based
   on the POST body isn’t something we can very easily implement. So we have decide
   to not implement is.
 * There are however two possible workarounds you could use:
 * **Add a get variable**
    You could add a get variable to the POST url which is
   used to differentiate. For instance you could add a hash of the POST body.
 * **Cacheable request headers**
    You could create a hash of your POST body and 
   put it in a request header. Our plugin does support cache differentiation based
   on request headers. See: [https://wordpress.org/plugins/wp-rest-cache/#can%20i%20differentiate%20between%20caches%20based%20upon%20request%20headers%3F](https://wordpress.org/plugins/wp-rest-cache/#can%20i%20differentiate%20between%20caches%20based%20upon%20request%20headers%3F)

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Cache http post method’ is closed to new replies.

 * ![](https://ps.w.org/wp-rest-cache/assets/icon-256x256.png?rev=3328849)
 * [WP REST Cache](https://wordpress.org/plugins/wp-rest-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-rest-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-rest-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-rest-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-rest-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-rest-cache/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [HTTP POST](https://wordpress.org/support/topic-tag/http-post/)

 * 7 replies
 * 2 participants
 * Last reply from: [Richard Korthuis](https://wordpress.org/support/users/rockfire/)
 * Last activity: [3 years, 6 months ago](https://wordpress.org/support/topic/cache-http-post-method/#post-16152839)
 * Status: resolved