简体   繁体   中英

HTTP status code. What is the best status code for an empty parameter?

I'm writing an API in Django Rest Framework.

I have a password reset endpoint that accepts one 'email' parameter. What if there is a key 'email' in the header, but the value was an empty string?

key: 'email'    
value: ''

What status code should I return? 400, or maybe 204?

EDIT: I forgot to mention that this shouldn't be a validation failure. No authorization is needed for this endpoint. This is only serves to send a password reset email.

Http statuses are just standards, you can adjust them to suit you. 204 no content is generally used when no response body is returned, so you want be able to respond from server with "no email provided whatever error massege". In DRF, 204 is returned (as default) after successful DELETE request, when entity was deleted at server, so there is no content left.

In your case I would go with 400. http://www.restapitutorial.com/httpstatuscodes.html is specifically mentioning "missing data" as an example of error 400 bad request usage, which might be exactly your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM