Let's say we have a service to add a new hotel:
> POST /hotel > <hotel> > <a>aaa</a> > <b>aaa</b> > <c>aaa.......this is 300K</c> > </hotel>
And then we have a get:
> GET /hotel < HTTP/1.1 200 OK < <hotel> < <a>aaa</a> < <b>aaa</b> > <c>aaa.......this is 300K</c> < </hotel>
Question is what do we return for the initial POST creation? We would like to return the ID (generated on the server) for a "reference" to the new resource but we don't want to return all the hotel data as in our case one of the data fields is a flat file of ~300K.
So should you just return:
< HTTP/1.1 200 OK < <hotel> < <id>123</id> < </hotel>
Or should you return the full object:
< HTTP/1.1 200 OK < <hotel> < <id>123</id> < <a>aaa</a> < <b>aaa</b> > <c>aaa.......this is 300K</c> < </hotel>
??
I'm interested in the restful best practice.
Note: this related post talks more about what to return but less about how to return it.
It doesn't matter if the resource returns an ID field. It is up to the client to keep a map of where it was obtained from. In a + b, the server would create a new resource.
Yes, even though it returns void, in a class which extends Resource, you have full access to the Response object object via the getResponse() method. So you can call getResponse().
The API should always return sensible HTTP status codes. API errors typically break down into 2 types: 400 series status codes for client issues & 500 series status codes for server issues. At a minimum, the API should standardize that all 400 series errors come with consumable JSON error representation.
Return the status code 201 - Created, and put the URL in the Location header. You don't need to return a body at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With