Could we not have an endpoint that receives only a single string (like / dislike), without having it encapsulated in a JSON object ? Is it still a RESTful design? (This post suggests that a string is considered valid JSON)
Yes, you can. There is no rule that says that a request payload must be an object, or even that a request payload must be JSON. On the web, many request payloads are application/x-www-form-urlencoded
key value pairs.
The trade off is that you can extend the schema of an object by adding more "optional" fields, without breaking backwards compatibility.
Isn’t it a code smell to have a REST endpoint that receives only a single-field information (and, in this case, actually a binary one: true/false)?
Yes - in the sense that a "smell" indicates that there might be a problem somewhere (see https://wiki.c2.com/?CodeSmell ).
There's nothing wrong with having a document that has but a single field of information in it.
Similarly, there's nothing wrong with making such a document available via the web; there's nothing wrong with supporting changes to that document from the web.
Things get a little bit suspicious when you send edits to one resource expecting the effects to show up somewhere else. For example, if we are submitting
POST /api/great_new_feature/feedback
In the expectation that it will change what we see when we
GET /api/great_new_feature
Then that deserves further review, because it impedes the general purpose cache invalidation mechanism built into HTTP. If you are giving up something that is "free", then you better be sure that you are being compensated. In a code review, I would expect to find a decision record documenting the investigation and its findings.
I’m not sure I understand the last paragraph about cache invalidation. Is it specific to an API expecting a single field of information ?
No. HTTP message semantics are part of its uniform interface -- all resources understand messages the same way.
And what is a generally accepted solution to this problem? (I see that the whole website I work on has a Cache-Control: no-store policy)
Ensure that requests that modify information use the same target URI as the requests that read information.
POST /collection
Is a popular choice for introducing new items into a collection because (in part) we expect that the introduction of the new item also changes the representation of the collection itself (typically with a link to the new item).