axios-extensions icon indicating copy to clipboard operation
axios-extensions copied to clipboard

Requests with POST is not cached

Open AshwinTayson opened this issue 6 years ago • 14 comments

POST requests are not cached. It is requested for every request. Is there some limitation or Do I need to configure it differently?

AshwinTayson avatar Nov 09 '17 07:11 AshwinTayson

POST requests are not supported, and I think to fetch data with POST request was not a good idea...

kuitos avatar Nov 09 '17 08:11 kuitos

closed

kuitos avatar Nov 16 '17 12:11 kuitos

Hi @kuitos

I think sometimes it makes sense to cache POST requests.

One example I can find is

POST https://graph.microsoft.com/api/beta/me/getMailTips

{
    "EmailAddresses": [
        "[email protected]", 
        "[email protected]"
    ],
    "MailTipsOptions": "automaticReplies, mailboxFullStatus"
}

It returns Jack and Rose's mailtip.

BTW, thanks for brining us this library!

hongbo-miao avatar Mar 15 '18 18:03 hongbo-miao

This api was served by graphql?

kuitos avatar Mar 16 '18 06:03 kuitos

No, it is REST API. Sorry, forget the link. https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/user_getmailtips

hongbo-miao avatar Mar 16 '18 07:03 hongbo-miao

@Hongbo-Miao Basically POST is not an idempotent operation in semantics. So caching for post request will not be provided imo. See https://stackoverflow.com/questions/626057/is-it-possible-to-cache-post-methods-in-http

kuitos avatar Mar 19 '18 14:03 kuitos

@kuitos got it, thank you for the info!

hongbo-miao avatar Mar 19 '18 18:03 hongbo-miao

For the sake of discussion:

Technically the HTTP/1.1 RFC document says that POST is a cacheable method: https://tools.ietf.org/html/rfc7231#section-4.2.3. MDN also says it is cacheable if freshness information is included: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST

In my case I need to send data to be analyzed (as a big JSON) to the server in order to get a response (which can take several minutes to process) - I'd like to cache that response as long as the request body is the same. I don't mind having to manually forceUpdate, but it would be super nice to be able to cache the response to a POST.
It's also possible that this pattern shouldn't be a RESTful interface..

I guess it might be a big change though, since it looks like this library is really structured around GETs and the buildURL helper - how would we handle the POST body if we were to update this project, right?

Anyway I'd like to help but it's a bit over my head. If anyone can give me a few pointers, I can work on it and try to contribute. Thoughts?

brendanmatkin avatar Aug 14 '19 07:08 brendanmatkin

@brendanmatkin Thanks for your valuable information! As more than one person had asked for the post cacheable feature, we may need to provide a mechanism to make user can define the cacheable logic by himself. I'm going to think about how to do that in the near feature, and you can make any suggestion at any time.

kuitos avatar Aug 14 '19 07:08 kuitos

@kuitos ok thanks! I'll chew on it.

For now I'm thinking (since it seems to be needed in only a few edge cases), that checking if the body has changed happens in the implementation (maybe using force-update), and axios-extensions just allows us to use POST as a method. (I guess this is probably what you mean by "define the caceable logic by himself").

This would probably be way easier to build but might lead to some confusion when things are automatically caching even though something has changed... maybe we add another option to flag it as manual caching logic?

brendanmatkin avatar Aug 14 '19 17:08 brendanmatkin

@kuitos I feel a generic library like "axios-extensions" should cater to all the methods that axios supports without introducing any subjective presumptions about any methods. It should be on the user to determine which rule of theory they follow. It should let the user choose ingredients of the hash e.g. query, params, body, headers etc. You could just have "cache":true and additional hashing inputs from user.

Doing that would give this library a more generic, free from any assumptions.

abhipanda avatar Sep 26 '19 16:09 abhipanda

@kuitos have a look a cachios, it has a nice implementation of it. mostly due to being able to cache the post request/response with a custom key you want Good work though!

lkounadis avatar Nov 29 '19 09:11 lkounadis

POST requests are not supported, and I think to fetch data with POST request was not a good idea...

I've created an package at npm to cache post and get request, just look for axios-post-cache

gustavo-h-freitas avatar Oct 27 '20 13:10 gustavo-h-freitas

@brendanmatkin Thanks for your valuable information! As more than one person had asked for the post cacheable feature, we may need to provide a mechanism to make user can define the cacheable logic by himself. I'm going to think about how to do that in the near feature, and you can make any suggestion at any time.

Excuse me, has this function been added?

gongjianh avatar Jun 13 '22 03:06 gongjianh