Upon trying to access the content of a successful HTTP POST request's body in a Response, I encounter the following error:
SyntaxError: Unexpected end of JSON input.
Here is the Express.js code snippet I used to send the response:
res.status(204).send(response)
Even though Express's res.send()
function automatically converts JavaScript objects to JSON during transmission, the variable response
is explicitly being converted to JSON using JSON.stringify()
within its definition. Thus, JSON data is indeed being sent.
--
On the client side, I handle the request as follows:
fetch(URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data) // data is defined elsewhere in the file
})
.then(res => res.json())
.then(data => console.log(data))
Yet, whenever I try to fully read response.body
(referencing Body), I encounter the same error:
SyntaxError: Unexpected end of JSON input.
It has been confirmed that the intended data transfer from server to client is indeed in JSON format. Therefore, the issue lies in the process of conversion during transmission, where non-JSON content is being erroneously treated as JSON.
I suspect that the data I am sending is getting misplaced during transit, but I am uncertain about the cause.