• Resolved emanuelx

    (@emanuelx)


    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

    (@rockfire)

    Hi @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

    (@emanuelx)

    Hi @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

    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

    (@rockfire)

    Hi @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

    (@emanuelx)

    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

    Request number 2
    curl -X POST -H “Content-Type: application/json” -d ‘{“slug”: “test-1”}’ 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

    (@rockfire)

    Hi @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

    (@emanuelx)

    Hi @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

    (@rockfire)

    Hi @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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Cache http post method’ is closed to new replies.