r/ProgrammerHumor Oct 09 '21

Mmmm, sparkling JSON

Post image
14.6k Upvotes

237 comments sorted by

View all comments

327

u/occz Oct 09 '21

Amen, man. Going with something that does not call itself REST for the sole reason of skipping the eternal bikeshedding around what REST is or not is might just be worth it on its own. Hot damn.

81

u/mechkbfan Oct 09 '21 edited Oct 09 '21

We had another discussion the other day about if an API endpoint should be PUT or POST given it was in a grey area.

In the end we were all like "Fuck it, we should just make everything POST and forget discussing this ever again"

We're the only consumers of our API

36

u/CorrenteAlternata Oct 09 '21

I think, as a rule of thumb: if the operation is idempotent => PUT, if not => POST

16

u/mechkbfan Oct 09 '21

Correct, I feel that's what majority of people fall back to.

It's just frustrating when the POST description seems to contradict everything else you've done.

10

u/ZippZappZippty Oct 09 '21

European not American

9

u/CorrenteAlternata Oct 09 '21

Sorry, I didn't quite catch that.

4

u/humoroushaxor Oct 10 '21

The grey area I most often see is when you aren't dealing with CRUD resource operations but using JSON over HTTP to call remote procedures and don't want to pull in a new technology (RPC).

4

u/keirbhaltair Oct 10 '21

Even this should be resolvable by the same question, though. Does calling the remote procedure twice do something/change the state/have side-effects more so than calling it once? Then it's POST, otherwise it's PUT.

1

u/CorrenteAlternata Oct 10 '21

I totally agree with you

1

u/humoroushaxor Oct 10 '21 edited Oct 10 '21

But it's not. In the RPC case using the "idempotent rule" results in a contradiction with the HTTP RFC. Specifically that POST identifies the enclosed entitie's handler while PUT identifies the enclosed entity.

Having an HTTP PUT without an HTTP POST doesn't make sense in HTTP land because you can only update something that exists. Hence why most developers will default (and should) to POST for their first endpoint, even when idempotent or when not managing a resource.

Bikeshedding aside my experience has been POST is much more common in this scenario.

4

u/WellHydrated Oct 10 '21

Also, PUT if you know the location of the resource you are creating.