Unexspected end of JSON input

Hey guys, I recently updated from API Plugin v6.1.1 to v7.0.3 and got into trouble with the new handling of request body. I try to extract the body of my request with const body = await request.readBodyJson(); Once this call is executed I get the error SyntaxError: Unexpected end of JSON input. My JSON looks the following
{
"action": "app_membership.went_invalid",
"data": {
"created_at": 1697500033,
"expires_at": null,
"renewal_period_start": null,
"renewal_period_end": null,
"quantity": 1,
"status": "canceled",
"valid": false,
"cancel_at_period_end": false,
"license_key": null
}
}
{
"action": "app_membership.went_invalid",
"data": {
"created_at": 1697500033,
"expires_at": null,
"renewal_period_start": null,
"renewal_period_end": null,
"quantity": 1,
"status": "canceled",
"valid": false,
"cancel_at_period_end": false,
"license_key": null
}
}
I don't know how to deal with this error as I already tried everything I know to fix this. Any suggestions or ideas would be recommended πŸ™‚
Solution:
You shouldn't read the body twice πŸ˜…
Jump to solution
4 Replies
kyra
kyraβ€’4w ago
You have a leading comma at license_key, which is a syntax error in JSON, and you’re not closing the top brace ({})
ShowCast
ShowCastOPβ€’4w ago
Good point. I will correct this is only a Copy & Paste Error here sorry πŸ˜… This was not the error, I fixed the example JSON of my post as I was using the "correct" version on Postman Funny thing seems like if (((await request.readBody()) as any).action !== 'app_membership.went_invalid') return response.badRequest(); works but when I want to access .data I get an exception
Solution
kyra
kyraβ€’4w ago
You shouldn't read the body twice πŸ˜…
ShowCast
ShowCastOPβ€’4w ago
Well that's a good point! You are absolutely right, I transformed my code (In v6 I accessed request.body and so on) and now it's working πŸ˜„ Thx :this:
Want results from more Discord servers?
Add your server