1

I am checking whether a particular email address already exists in the database or not, an AJAX function sends the email address for the server to check.

What is the best way for the server to respond? Should the response be just true or false or maybe I should be sending some specific response codes?

Michael Victor
  • 861
  • 2
  • 18
  • 42

2 Answers2

2

The response code isn't really meant to indicate the result of your business logic; the response code indicates the success or failure of your request to the server.

This would mean that any successful request to the server should get a response of 200 plus a payload representing the result of that response (in your case, true or false).

Leave the error status codes to be handled by your hosting service to avoid confusion (ex: let's say you choose that a 404 should represent "email not found", but the service goes down or there's a bug in the JavaScript which causes a request to the wrong uri...)

dshapiro
  • 376
  • 2
  • 12
  • "The response code isn't really meant to indicate the result of your business logic" — This doesn't make much sense to me. In REST, CRUD operations should give various codes (`201 Created`, `422 Unprocessable`, `400 Bad Request`) — all of which may depend on whatever business logic is implemented. What one app may define as a "bad request" may be fine for another app. Nevertheless I agree that the appropriate status code for this is probably `200 OK` with a simple "true" or "false" payload, simply because HTTP has no good codes for "Yes, this exists" or "No, this does not exist". – Sammy Taylor Aug 07 '19 at 21:23
0

I prefer use server code, in this case probably I would use 200: if email not exist (equals true value), 401 conflict if email is present (equals false value) and response body empty.

GeekRiky
  • 375
  • 3
  • 12