[Catalyst] RESTful response codes.

Bill Moseley moseley at hank.org
Fri Feb 24 04:47:14 GMT 2012


On Fri, Feb 24, 2012 at 10:08 AM, Dave Rolsky <autarch at urth.org> wrote:

>
> One thing that can be a bit though is some sort of batch update, where you
> want to update more than one thing at once for efficiency. What do you do
> if part of the batch succeeds and part fails? Maybe you just run everythi=
ng
> in a transaction so this isn't possible.
>

Yes, that's a good example.

The transaction part is up to the business logic.   If the logic is
All-or-None then it's 200 or 400, right?

As and example, for batch file uploads the business logic allows some to
fail, so I return a response and a way for the client to map what they
uploaded to the items in the response (e.g. the response keyed by
client-provided filename).  Some may be accepted and some may be rejected
with an error: "sorry, .exe files are not accepted".

So, that's a 200 response with the response structure that indicates what
was done with each item in the batch.  The request worked as expected --
and it's expected that some items may fail.

Now, If sending a batch of items vs. a bunch of pipelined single POST
requests is much faster is another question.  And if not, then life might
be simpler, eh, more restful, if only allow one upload at time.


For a batch update I'd tend to fail the entire thing, but it depends on the
business logic.

Say, to *set* the set of tags associated with a photo:

PUT /photo/123/tags  { tags =3D> [ 1,2,3,55,9 ] }

And "55" doesn't exist I'd probably fail 400 because you cannot complete
what the request requested.

So, likewise if each element happes to be more complex (say updating a
collection of users) then it's still the same.



BTW -- I'm lazy so I always return a hash with a key and a value of a LIST.

GET /user   # returns ALL users

returns a LIST of users:

 {  user =3D> [ \%user1, \%user2, ... ] }

So, what I do for convention is ALWAYS return a list, so:

GET /user/123 returns { user =3D> [\%user123] }

... which makes coding easy on both ends.  And having it keyed by "user"
means I am free to  combine requests to return additional data in the same
response structure, of course.

Yes, there's gray area.


-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20120224/c08d5=
43c/attachment.htm


More information about the Catalyst mailing list