Can HTTP GET be used if there are small side-effects?
Asked Answered
B

1

8

As far as I understand, GET has to be idempotent - it cannot change anything in the system and repeating it should always return the same unless something else changed the state of the application.

Problem is, on my website, there should be a lot of little changes based on whether some content has been already seen before by the user. I mean, like notifications, new message alerts, content sorting based on whether user saw it or not, how many users saw the content counters... For example when user clicks on notification, the notification should disappear from his notification panel and never be seen there again, so it changes something - state of the notification, but I can press F5 as many times as I want and it always returns the same page with same info, same buttons etc.

Can I ignore this kind of side-effect as too small and I can use GET or do I have to make most links and buttons on the website use POST like I do with links and buttons which lead to more "serious" changes? I do that by making forms around each of them with those links as form actions and original buttons as submit buttons of the form which seems a little messy to me and I don't want it almost everywhere (or is there a better way?).

Barbarism answered 28/12, 2016 at 9:45 Comment(2)
The more important question is whether the request is safe, see greenbytes.de/tech/webdav/rfc7231.html#safe.methodsKaliski
This related answer provides some insights based on RFC 7231.Kiarakibble
S
8

This can be tackled from several points of view, I'll illuminate it from this angle:

The end result here is always the same, hence the request is idempotent.

The user visits the URL, the end result is that the notifications are cleared. They can visit the page again and again, the end result is that the notifications are cleared. They're not going to get an error message because the notifications have been previously cleared (that would not be idempotent). Yes, they will see different content each time they visit the page, but nothing says that the page content must be identical each time the resource is requested (that would put half the web in a bind).

Contrast that with a POST request to /questions (which creates a new question, for example): each time you repeat that POST request, a completely new resource is being created. You POST once, and /questions/12345 is being created. You repeat the same request, /questions/12346 is being created. That is not idempotent.

No, you do not need to make all those requests into POST requests.

Spelling answered 28/12, 2016 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.