Get response code ? 404 or 200 with empty body+ content length = 0

36 views
Skip to first unread message

Natraj M

unread,
Nov 24, 2014, 6:08:13 PM11/24/14
to restinp...@googlegroups.com
Hi All.

I need your thoughts on REST Get response.

Scenario : let assume we Books as Rest Resource where we can do CRUD operation.
  • Get /Books  will return  with 200 Ok with all the book list we have as response 
  • Get /Books/1234  will  return based on below use case
    • Case 1. If we found the book with Id = 1234 then we send 200 OK and book details in response body
    • Case 2. If NOT Found  then ?
      • Option 1. reply with 404 with response body = "Book with Id = 1234 Not Found"
      • Option 2. Reply with 200 OK with Content Length = 0  and/or  response body = "Book with Id = 1234 Not Found "

Based on my thought option 2 is right way to do as 404 is for resource and not for individual entities that are mapped to Resource.

Thanks You,
Natraj M.
 


Savas Parastatidis

unread,
Nov 24, 2014, 7:46:15 PM11/24/14
to restinp...@googlegroups.com, Natraj M
/Books/1234 is a resource. HTTP 404 is absolutely the correct response. By returning HTTP 200 with no content to requestors you are effectively saying that the resource is there but has no content.

.savas.

--
You received this message because you are subscribed to the Google Groups "restinpractice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to restinpractic...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joshua Graham

unread,
Nov 24, 2014, 10:16:58 PM11/24/14
to restinp...@googlegroups.com, Natraj M
Agreed. This is pretty straightforward basic stuff.

However, I do find some people wanting to differentiate in an API between an invalid path and a path that "looks right" but references some identifier that doesn't exist. This comes about from a number of flaws in thinking:

1) /Books is a resource. /Books/1234 is a resource. There is nothing other than our pre-conceived notions about Unix directory paths that says they are in any way related. URLs are opaque.
2) There is an implicit assumption that the part of the path following /Books/ is the id of a book. That is, it's a URL template. URLs are opaque.
3) Even if there is a relationship between the two resources, any explicit hypermedia links between them establish that relationship. If I were given the URL /Books then I could use a hypermedia link to traverse from there to any of its collection of books, which includes the one with an id of 1234. If I were given the /Books/1234 URL then I could use a hypermedia link to traverse up to the collection list. What does the "H" in HTTP stand for?

Now, if you want an error to differentiate between a path that's not valid in your API and a non-existent identifier, you still return 404 but possibly add a response entity to the invalid path case along the lines of:

Req:
GET /Bools/1234

Resp:
404 Not Found

Invalid path: /Bools


Req:
GET /Books/1234

Resp:
404 Not Found

No book with id: 1234


Regards,
Josh
Reply all
Reply to author
Forward
0 new messages