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.
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).
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).
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).
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.
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?
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...
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With