GET or POST in API

Hey, quick programming question because we’ve got a heated discussion in company.I’ve got a REST endpoint that fetches data about the number of kids in specific facilities within a given date range (e.g., from August 1st to August 31st). Right now, I’m using POST, and in the request body I pass an object that contains a list of facility IDs and the date range (from / to). The discussion started because some people say it should be a GET instead of a POST, but in my opinion POST makes more sense here. Otherwise, I’d have to either pass dozens of companyId values in the URL or call the endpoint multiple times, which feels wrong. So what do you think — are you team POST or GET here? Or maybe there’s a secret third option I don’t know about?
No description
18 Replies
JavaBot
JavaBot5d ago
This post has been reserved for your question.
Hey @Slownik1! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
LinusHuck
LinusHuck5d ago
POST is for creating stuff Otherwise, I’d have to either pass dozens of companyId values in the URL or call the endpoint multiple times, which feels wrong. No you can just use a body to pass all the input you want not recommended but possible, but as query params would be more the norm
No description
LinusHuck
LinusHuck5d ago
So yes GET would be correct
ayylmao123xdd
ayylmao123xdd5d ago
you can pass lists as a parameter in get requests and its frontends job to prepare the parameters properlty
fschmidt4
fschmidt45d ago
POST is obviously cleaner, but then modern programmers disagree with me on everything. Team POST should consider rejecting modern programming and modern culture.
2fb2cf
2fb2cf5d ago
This is wrong in two ways: - Post is not just for creating stuff, it can also be used for everything that doesn't fit any other method - By the spec GET should not have a body
LinusHuck
LinusHuck5d ago
So you want to GET some Information about kids in specific facilities than you should use a GET By the spec GET should not have a body did you even read my response? not recommended but possible next sentence query params would be more the norm Post is not just for creating stuff it commonly used to do it and in your usecase its def a GET you would use a post if your query would get to long (over 8kb)
Madjosz
Madjosz5d ago
It's not like you need to write the query by hand.
LinusHuck
LinusHuck5d ago
Also true you can input it here
No description
ayylmao123xdd
ayylmao123xdd5d ago
how is it cleaner
fschmidt4
fschmidt45d ago
Long URLs are ugly. And JSON can be logged for debugging.
ayylmao123xdd
ayylmao123xdd5d ago
url will get ugly for who backend doesnt even view the url and frontend builds the url with parameters not manually and u can debug the parameters aswell ez
fschmidt4
fschmidt45d ago
URLs show up in error logs, for example. Ugliness should be avoided as a matter of principle.
ayylmao123xdd
ayylmao123xdd5d ago
so what if they show up in error logs its not like the url gonna have 500 parameters in this example and get is specifically made for querying data so im absolutely right
fschmidt4
fschmidt45d ago
Dozens is bad enough. Decent people should violate the rules of modern programmers and modern culture whenever possible.
ayylmao123xdd
ayylmao123xdd5d ago
you forgot to add 'and then complain about being fired for unmaintainable code' ahahahah
Madjosz
Madjosz5d ago
I am starting to get (🥁) why.
JavaBot
JavaBot4d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?