Reputation: 482
Can a http service return HTTP 400 (bad request) if a required parameter is missing? When I read around it seems that http 400 should only be returned on syntax errors in the request, but can I treat a missing argument like this?
Upvotes: 2
Views: 2122
Reputation: 52538
Are you asking from the client's point of view or the server's point of view? From the client point of view, a server can return any error for any reason whatsoever; it's out of your control. A good server will return an error 400 in a situation where it tells you that it's not ever going to fulfil the request because it thinks there's something wrong with the request. The only solution to an error 400 is reading the spec for requests to the server and fixing the request in your code. If a required parameter is missing, surely you will have to read the server's spec and fix your code, so error 400 is (kind of) appropriate.
If you write server code, you should always return the error that is most appropriate, keeping in mind that the error is reported with the intent that either a client can report the problem to the user, or a client can try to fix the problem or handle it appropriately, or the client may need code changes to fix the problem. Reporting incorrect error codes will likely confuse clients or force client codes to apply hacks when they access your server, with the risk that these hacks break when the server code is fixed. That said, I cannot find an error code that would be obviously more correct than 400 for a missing required parameter.
Upvotes: 1