Wednesday, July 18, 2012

HTTP Status Codes for REST

For REST services, one common pattern is to use HTTP status codes as the status code for the request itself.

A good reference: http://restpatterns.org/HTTP_Status_Codes

2 comments:

  1. Simple use-case: cross domain javascript
    you will not be able to read the REST header code if you return anything else than 200, you can use special header if you want
    like X-API-Error: 453 or body error {"error":"123456","errorContext":"Big problem sir!"}

    jquery and other libraries wont be able to return you the error, you won't even know there was an error the query will just fail

    Cross domain js is emulated using iframes kind of mechanism and if you return 400 your iframe wont load so the jquery callback wont be called and it will just fail silently.

    The only reason I recommend for returning non 200 errors is when the request is not properly formated (i.e. not follwoing the protocol/contract like missing parameters and stuff)
    missing param -> HTTP 400
    invalid param value -> HTTP 200 + HTTP Header error code or payload API error code
    That means that whoever built the client didn't follow the protocol

    ReplyDelete
  2. Yes, that's a good point. Thanks!

    ReplyDelete