Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST-way to control whether to return representation of created resource in POST response

Tags:

rest

Is there a "REST-way" by which a client of a REST-API can indicate whether it is interested in getting a resource representation of the created resource in the POST response or whether it is just interested in HTTP response code (i.e. 201 on success) and location header?

I was thinking on using the Accept header. If the client adds a media type to this header that is supported by the API, the representation will be returned. Otherwise, it will just get a HTTP Created and the location header.

Context: The API will have to types of clients. A Web-UI client, that wants the representation in the response to save a network round-trip. And backend-service clients that will create resources at high rate in fire-and-forget manner. They're only interested in response code and resource URI.

like image 864
Christof Schablinski Avatar asked Jan 25 '26 11:01

Christof Schablinski


1 Answers

There is a HTTP header:

Prefer: return=representation

The server can signal that it respected the header by returning:

Preference-Applied: return=representation

https://www.rfc-editor.org/rfc/rfc7240

like image 109
Evert Avatar answered Jan 28 '26 07:01

Evert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!