Question

Given a RESTful server backend that uses JSON encoding for objects. The GET request return the object like { id: 42, name: 'Peter', age: 28 }. Now I wonder whether the POST request, which inserts a new object, should return the whole object in the same form, or just return the created id.

For example the client sends this request to create a new entry in database.

method:  POST
url:     http://example.com/persons
type:    application/json
content: { name: 'Max', age: 35 }

Should the server respond with { id: 43, name: 'Max', age: 35 } or just with the created id like 43 or { id: 43 }?

In this minimal example, I guess bandwidth wouldn't really matter, but actually, there are longer properties encoding geographical areas involved.

Was it helpful?

Solution 2

I am not aware of any guidelines. Personally, I think it may be useful to return the data the client cannot know – primarily the new ID, of course, but the server may have canonicalized or otherwise changed other data as well.

But in the end, it just depends on what the client wants to do with the data. If returning anything at all, I'd use a format that is open for further extension, not just a numerical ID or anything like that.

OTHER TIPS

REST is a architectural design there are no hard and fast rule about what the response data should be

A create operation should ideally return the URI back to the caller so I would say whatever is the identifier, returning that would be the best practice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top