Reputation: 3797
I want to make a database query from frontend (Angular) to backend. But I need to send lots of parameters for that.
As far as I understand, if we are not making any database changes, it is better to use GET as it uses cached entries. POST should be used used if we need to make changes on server/DB.
But if I want to send many parameters (some are serialized objects) and make no server side changes, will it be alright to use POST request in that case and embed all parameters in the POST body instead of sending a huge URL encoded GET request?
Upvotes: 1
Views: 817
Reputation: 290
I think you should use POST
in this situation which is more manageable and looks clean. For more benefit of post follow these links:
Upvotes: 0
Reputation: 151586
To first clear this up: responses to POST requests can be cached, as long as the origin server returns the proper caching response headers. However, browsers and proxy servers generally don't apply caching to POST requests.
That being said, with the proper encoding you can store a lot of information in the ~ 2 KB of a query string, so GET should be the way to go.
If you're certain you'll go beyond the limits of a GET request and you'll need to go the POST way while remaining cacheable, you could introduce a kind of "nonce", for example using a hash of the search parameters:
/search
, with the search parameters./search/123abc
./search/123abc
search results page.This will introduce one additional HTTP roundtrip, but enable caching cross-browser and through proxy servers.
Upvotes: 1