Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the appropriate HTTP response for invalid data submitted by the user?

I'm experimenting with JSON and http response codes. I'm submitting a form via an AJAX request and I obviously need to validate the data on the server-side.

My idea is to respond with a "200 OK" response (with a confirmation message as the body) if the post is successful. I don't know what to respond with if the data that the user sends is invalid.

like image 451
rich97 Avatar asked Sep 07 '11 20:09

rich97


People also ask

What does invalid HTTP response mean?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

When should 422 be used?

A 422 status code occurs when a request is well-formed, however, due to semantic errors it is unable to be processed. This HTTP status was introduced in RFC 4918 and is more specifically geared toward HTTP extensions for Web Distributed Authoring and Versioning (WebDAV).

What is the status code for invalid data?

HTTP status codes the server can generate in response to HTTP requests: 200 OK : Successful request. 400 Bad Request : Invalid argument (invalid request payload). 403 Forbidden : Permission denied (e.g. invalid API key).

Which are valid HTTP responses?

After receiving and interpreting a request message, a server responds with an HTTP response message: A Status-line. Zero or more header (General|Response|Entity) fields followed by CRLF. An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields.


5 Answers

You could send a 400: Bad Request header. If that's not your cup of tea, maybe check through the W3C's Status Code Definitions?

like image 197
Nightfirecat Avatar answered Oct 05 '22 02:10

Nightfirecat


Just implement a standard protocol like JSON-RPC. It has error handling, parameter passing, etc.

Request:

{"method": "postMessage", "params": ["Hello all!"], "id": 99}

Response:

{"result": 1, "error": null, "id": 99}

And on error:

{"result": null, "error": "Duplicate Message", "id": 99}

It's quite flexible, and is standard...

like image 31
ircmaxell Avatar answered Oct 05 '22 01:10

ircmaxell


Send back a JSON object:

$message = array(
   'error' => true,
   'code' => 'some error number relevant to you',
   'message' => 'A nice human-readable+relevant error message'
);

echo json_encode($message);

I prefer signaling errors with a service in this way. Fiddling with HTTP status codes doesn't seem right, as EVERYTHING about the actual HTTP request itself worked fine - it's just that the request didn't conform to the service's expectations.

like image 26
Marc B Avatar answered Oct 05 '22 01:10

Marc B


Here's the complete list of HTTP status codes. The first one that springs to mind for your situation is 400 Bad Request, but that's usually used to indicate an error in the HTTP syntax rather than an error in the body content. Still, without more information I'd go with that one.

In specific cases, depending on the exact nature of the data you're receiving, I could see any of 403, 404, 410, 413, or perhaps others being the appropriate response.

like image 45
David Z Avatar answered Oct 05 '22 00:10

David Z


Depends on the purpose of API. If it's yours (private) then answer with HTTP status 400 as Nightfirecat suggested. If it's a public API send a meaningful error message to aid developers.

like image 20
johndodo Avatar answered Oct 05 '22 00:10

johndodo