3

What would be the appropriate status code to use for a server telling a user who is signing up using the site's REST API that their password did not meet the specified requirements (such as length)?

I'm thinking either 400 or 422 based on the answers to this question.

My application currently has no frontend and is entirely an API.

Zeke Marffy
  • 188
  • 3
  • 14
  • I think sending an HTTP response code is not the right approach. Why not just set up a text label on your page somewhere to be activated if the REST API returns that the password was not valid? – Russ J Dec 22 '19 at 00:38
  • @RussJ Definitely a valid point. It's just that currently my application has no frontend and is entirely an API. – Zeke Marffy Dec 22 '19 at 00:40
  • I would suggest adding that to your question then. – Russ J Dec 22 '19 at 00:46
  • I guess the HTTP code doesn't matter much in this case. I guess a 400 error would be better since I guess every developer knows it and wouldn't have to look it up. – maio290 Dec 22 '19 at 01:18

1 Answers1

2

What would be the appropriate status code to use for a server telling a user who is signing up using the site's REST API that their password did not meet the specified requirements (such as length)?

The status code is not the right tool to use for this

the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.

The message body is how we communicate with the user; consider the web, information about an inadequate password would be in the HTML message sent back to the browser to be rendered for the user.

The status code is metadata, it belongs to the transferring-documents-over-a-network domain, to give to general purpose components a standardized understanding of what is going on, so that they can act appropriately (for example, status codes play an important role in cache invalidation).

The most straightforward choice to make in your case would be 403 Forbidden

The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it.

422 Unprocessable Entity is fine, especially in that it directs attention to the message-body of the request.

In practice, it probably doesn't matter very much -- besides the minor detail of calling attention to the problematic part of the HTTP request, 400, 403, and 422 are effectively interchangeable. They are all members of the Client Error (4xx) class, they are all not cacheable by default, and so on.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91