3

I'm making some REST services, and as I'm implementing a GET request, I was debating about what I should do if there is no record there, for example /profile/1234. Now, if you tried to use /profiles/1234 (note the s on profiles) it would definitely be a 404 code because that URL definitely isn't found. But when it comes to there just being no 1234 profile, I'm not sure what I should return. I don't want to return {} because that might give the impression there is a record with no data. But if I return a 404 error code, how is the consumer of the API supposed to tell the difference between the two 404s?

How can I be a responsible API designer by communicating programmatically what the difference between a service that doesn't exist and a record that doesn't exist?

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • Looking through the list of http statuscodes, it seems that 204 might be the appropriate code for /profile/1234 – Erich Kitzmueller Sep 21 '14 at 07:59
  • I don't know about that. A 204 indicates that there is no body to return, yes - but why would I expect a 204 on a GET? I'd expect a 200 instead. – Makoto Sep 21 '14 at 08:00
  • 1
    possible duplicate of [What is the proper REST response code for a valid request but an empty data?](http://stackoverflow.com/questions/11746894/what-is-the-proper-rest-response-code-for-a-valid-request-but-an-empty-data) – Makoto Sep 21 '14 at 08:06
  • 1
    Stumbled across the duplicate when attempting to build my argument in favor of using a 404 instead of a 204. Seems like my intuition was right on this one. – Makoto Sep 21 '14 at 08:07
  • 1
    I accept the "majority decission" to return 404, but I'm not really happy with it. 404 might lead to hidden bugs where a routine incorrectly assumes that a record doesn't exist when in fact there is a typo in the URL. – Erich Kitzmueller Sep 21 '14 at 10:49
  • @Makoto Unfortunately, that doesn't answer this question. That question says "what code should I use" - my question is "since I can't use status codes, what else SHOULD I do?" – corsiKa Sep 21 '14 at 16:27

1 Answers1

1

You can send a custom status message which will help you differentiate between the "different" 404s.

On the other hand, I wouldn't worry about that distinction. People are used to having to type api endpoints carefully. As long as they manually test their app a tiny bit the issue becomes a non-issue.

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111