Commit a3be915
committed
Correct 400 response to malformed json in POST
Symptom:
----------
Trying to create an object sending a seemingly correct JSON request in a
POST message - was giving a 404 not found error. When looking at the cause
in the browser devtools - the body of the response contained "Not found (input)"
Cause:
-------
We were trying to validate parameters - even when the POST body
contained invalid JSON. We call json_decode($data) in retrieveInputs
The JSON request was malformed and it had omitted the double quotes.
example: { fieldName : "fieldValue" }
Expected behavior and fix:
---------------------------
A POST request with malformed JSON should get a 400 "bad request"
response. Even more helpful would be an error code indicating why the
JSON was bad.
If we call json_decode(), afterwards, we immediately call
json_last_error(), and if the return value does not match
JSON_ERROR_NONE, we send it across bundled in a 400 response, so that
the user may rectify the cause by looking at the exact error message.
The possible error messages right now are:
CODE CONSTANT-ERROR MESSAGE
0 JSON_ERROR_NONE-No error has occurred
1 JSON_ERROR_DEPTH-The maximum stack depth has been exceeded
2 JSON_ERROR_STATE_MISMATCH-Invalid or malformed JSON
3 JSON_ERROR_CTRL_CHAR-Control character error, possibly incorrectly encoded
4 JSON_ERROR_SYNTAX-Syntax error
5 JSON_ERROR_UTF8-Malformed UTF-8 characters, possibly incorrectly encoded
6 JSON_ERROR_RECURSION-One or more recursive references in the value to be encoded
7 JSON_ERROR_INF_OR_NAN-One or more NAN or INF values in the value to be encoded
8 JSON_ERROR_UNSUPPORTED_TYPE-A value of a type that cannot be encoded was given
9 JSON_ERROR_INVALID_PROPERTY_NAME-A property name that cannot be encoded was given
10 JSON_ERROR_UTF16-Malformed UTF-16 characters, possibly incorrectly encoded
After the fix:
-------------
On malformed JSON in the POST body - the following is returned:
The request could not be understood by the server due to malformed
syntax. The client SHOULD NOT repeat the request without modifications.
(Error decoding input JSON. json_last_error code: 4)
If REQUEST_METHOD is not set - we throw an exception that says something
like:
'Bad request (Error decoding input JSON. json_last_error code: 4)'
In order to accommodate this change in behavior - in the test suite, we
need a new method expectPattern (as the error code in the end of the
string may change, but the starting pattern stays the same - (/^Bad
request.*$/)1 parent b746794 commit a3be915
3 files changed
+33
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1270 | 1270 | | |
1271 | 1271 | | |
1272 | 1272 | | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
1273 | 1282 | | |
1274 | 1283 | | |
1275 | 1284 | | |
| |||
1684 | 1693 | | |
1685 | 1694 | | |
1686 | 1695 | | |
| 1696 | + | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
1687 | 1701 | | |
1688 | 1702 | | |
1689 | 1703 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
141 | 159 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
293 | | - | |
| 293 | + | |
294 | 294 | | |
295 | 295 | | |
296 | 296 | | |
| |||
0 commit comments