Invalid request payload JSON format
mgmorcos opened this issue · comments
Support plan
- is this issue currently blocking your project? (yes/no): no
- is this issue affecting a production system? (yes/no): no
Context
- node version: 20.6.1
- module version with issue: 21.3.2
- last module version without issue: ?
- environment (e.g. node, browser, native): node
- used with (e.g. hapi application, another framework, standalone, ...): hapi application
- any other relevant information:
What are you trying to achieve or the steps to reproduce?
I have a valid, large JSON example that complies with a Joi spec, which I verified as follows:
// array of large json documents
import inputExamples from '../data/input-data.json' assert {type: 'json'}
// joi schema
import inputSpec from '../specs/input.js'
// logs value and no error
console.log(inputSpec.validate(inputExamples[0]))
There is no error importing or validating the JSON example.
What was the result you got?
Sending the exact same validated JSON example to a Hapi endpoint results in a rapidly returned:
{
"statusCode": 400,
"error": "Bad Request",
"message": "Invalid request payload JSON format"
}
I have over-allocated maxBytes for payloads to ~100MB server-wide (the request body is ~1MB). I have enabled debug mode request logging (*). I have added logging to the route handler. I have added failAction handlers with logging to the route's payload and response validation configurations. The large request seems not to reach any of these handlers. Sending a much smaller JSON example to the endpoint works.
What result did you expect?
I expect the large request to reach the same handlers that the small request does, since they are both valid and under size limit.
I did some analysis with Bourne.parse and JSON.parse. The issue is unrelated to request size and is only related to JSON string formatting. Consider the following examples:
try {
JSON.parse(`{"foo":"bar\""}`)
} catch (e) {
console.error(e)
}
SyntaxError: Expected ',' or '}' after property value in JSON at position 12
try {
JSON.parse(String.raw`{"foo":"bar\""}`)
} catch (e) {
console.error(e)
}
No error
Is there any way to configure Hapi/Bourne to parse the JSON string as a raw string as in the second example? This would fix the issue.
After more analysis the issue originates in the API client (Yaade), not Hapi, so I have opened an issue there.