My application has a resource at /foo
. Normally, it is represented by an HTTP response payload like this:
{"a": "some text", "b": "some text", "c": "some text", "d": "some text"}
The client doesn't always need all four members of this object. What is the RESTfully semantic way for the client to tell the server what it needs in the representation? e.g. if it wants:
{"a": "some text", "b": "some text", "d": "some text"}
How should it GET
it? Some possibilities (I'm looking for correction if I misunderstand REST):
GET /foo?sections=a,b,d
.- The query string (called a query string after all) seems to mean "find resources matching this condition and tell me about them", not "represent this resource to me according to this customization".
GET /foo/a+b+d
My favorite if REST semantics doesn't cover this issue, because of its simplicity.- Breaks URI opacity, violating HATEOAS.
- Seems to break the distinction between resource (the sole meaning of a URI is to identify one resource) and representation. But that's debatable because it's consistent with
/widgets
representing a presentable list of/widget/<id>
resources, which I've never had a problem with.
- Loosen my constraints, respond to
GET /foo/a
, etc, and have the client make a request per component of/foo
it wants.- Multiplies overhead, which can become a nightmare if
/foo
has hundreds of components and the client needs 100 of those. - If I want to support an HTML representation of
/foo
, I have to use Ajax, which is problematic if I just want a single HTML page that can be crawled, rendered by minimalist browsers, etc. - To maintain HATEOAS, it also requires links to those "sub-resources" to exist within other representations, probably in
/foo
:{"a": {"url": "/foo/a", "content": "some text"}, ...}
- Multiplies overhead, which can become a nightmare if
GET /foo
,Content-Type: application/json
and{"sections": ["a","b","d"]}
in the request body.- Unbookmarkable and uncacheable.
- HTTP does not define body semantics for
GET
. It's legal HTTP but how can I guarantee some user's proxy doesn't strip the body from aGET
request? - My REST client won't let me put a body on a
GET
request so I can't use that for testing.
- A custom HTTP header:
Sections-Needed: a,b,d
- I'd rather avoid custom headers if possible.
- Unbookmarkable and uncacheable.
POST /foo/requests
,Content-Type: application/json
and{"sections": ["a","b","d"]}
in the request body. Receive a201
withLocation: /foo/requests/1
. ThenGET /foo/requests/1
to receive the desired representation of/foo
- Clunky; requires back-and-forth and some weird-looking code.
- Unbookmarkable and uncacheable since
/foo/requests/1
is just an alias that would only be used once and only kept until it is requested.