Get with json body will return Malformed JSON in request body
#4343
-
|
when i use a get and need batch query many params array (>1k) , i need put params in body . practiceRouter.get('/status-batch',
authMiddleware,
permissionMiddleware([Permission.QUESTION_PRACTICE]),
tbValidator('json', QuestionBatchGetBody, (result, c) => {
if (!result.success) {
return c.json({
success: false,
message: 'failed',
errors: result.errors
}, 400);
}
}),
async (c) => {
console.log('Batch get practice status');
const user = c.get('user') as JwtPayloadUser;
const body: Static<typeof QuestionBatchGetBody> = await c.req.json();
const result = await ExamQuestionService.getPracticeStatus(user.userId, body.questionIds);
return c.json(result, result.success ? 200 : 400);
}
);
// ------------------------
export const QuestionBatchGetBody = Type.Object({
questionIds: Type.Array(Type.String(), {description: 'ID list'}),
});when use postman to test it with follow body , will get a 400 error {
"questionIds": [
"68946d37efb4fb58c15aa0ed",
"68946d37efb4fb58c15aa0f1"
]
}but when i simple change it to node -v : v22.17.1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Yeah, your problem happened cuz get requests don’t normally have a body, and postman, along with probably most http clients, either strip it or don’t send it in a way that hono expect. It’s probably best to switch the route method to But if you really need to send a body with a get request, you'd need to bypass the default body parser behaviors. practiceRouter.get("/status-batch", async (c) => {
const raw = await c.req.text();
const body = JSON.parse(raw || "{}");
// ------------------------
});However, this would still probably fail on most clients that refuse to send a body with get requests. |
Beta Was this translation helpful? Give feedback.
Yeah, your problem happened cuz get requests don’t normally have a body, and postman, along with probably most http clients, either strip it or don’t send it in a way that hono expect. It’s probably best to switch the route method to
post, as body parsing works reliably with it.But if you really need to send a body with a get request, you'd need to bypass the default body parser behaviors.
However, this would still probably fail on most clients that refuse to send a body with get requests.