What should an API compatible REST server return?


#1

I’m having weird update errors on some routes so I just wanted to sure that it wasn’t something fundamental at this level.

At the moment I do this:

Create   POST       /plural        201    {"singular": {"id":"1", ...}} 
FindAll  GET        /plural        200    {"plural": [{"id":"1",... },{"id":"2", ...    
Find     GET        /plural/1      200    {"singular": {"id":"1", ...}} 
Update   PUT/PATCH  /plural/1      200    {"singular": {"id":"1", ...}} 
Delete   DELETE     /plural/1      200    (empty)

I serve 404s or 400s depending on whether the route looks good.

Is this to spec as far as you can see?


#2

This looks good to me. Did you see http://emberjs.com/guides/models/the-rest-adapter/?

If you did and found information missing, please consider sending a pull request to help flesh it out. :slight_smile:


#3

Yes. I’ve noticed a few things missing mainly what data to return on methods other than find (e.g. create). I will do - as soon as I work out how :slight_smile:

Thanks!


#4

If anyone is interested in a tiny PHP/MySQL ember-data compatible REST server, let me know. It’s a work in progress (and only deals with single requests/responses at the moment) but it’s good enough for people to have a play around with. You just create your database and point it at it and it works out the API from the database structure.


#5

Probably just a typo but shouldn’t POSTs go to /plural instead of /plural/1?


#6

Yes a typo I’ll fix. Thanks.


#7

This article suggests returning a 201 response with a Location header when a new resource has been created: http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/

In this case, your method can return either 204 if the resource exists or 201 with a Location header if a new resource has been created.

Also reading this wikipedia article claims that Location is valid for 201 responses: http://en.wikipedia.org/wiki/HTTP_location

However when I try this with the latest ember-data the Location header is not followed, there’s no GET request for the given URL.

Has anyone tried this? Is it ember-data specific or might there be something wonky with my response?


#8

I’m trying to integrate Ember Data in our app and I’m pretty happy with it so far. :smiley:

When creating records, our backend returns 201 along with a Location header (as per comment above). There is no body in the response.

This seems common behaviour to me, however the promise get rejected on record creation. A very similar problem has been reported here: http://stackoverflow.com/questions/21446352/success-callback-never-triggered-with-ember-data-save.

As a quick fix, we should be able to change the response returned from the backend, but shouldn’t Ember Data be able to deal with body-less responses too (if a correct Location header is provided)?

EDIT: I’ve found that the following (somewhat) solutions work:

  • Status code 201 and {} as body
  • Status code 204 and empty body

Can’t say I’m satisfied with any of that, will probably end up returning the full model later on.