honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
20.73k stars 606 forks source link

Request body with "multipart/form-data" throwing an http-exception error. #3713

Open vinay-khatri opened 4 days ago

vinay-khatri commented 4 days ago

What version of Hono are you using?

4.6.3

What runtime/platform is your app running on? (with version if possible)

Bun

What steps can reproduce the bug?

I am sending a multipart/form-data request from capacitor.js app in android to my hono backend. But my Hono app throwing an error upon receiving this request. My json post requests are working absolutely fine from this android app and our website. But formData request not succeeding for android app.

const submitReview = () => {
        let formData = new FormData();
        for (var key in review) {
            formData.append(key, review[key]);
        }
        formData.append("image", newImage);
        formData.append("service_id", id);
        fetch(`${string.Api_Url}/v1/service/review`, {
            method: "POST",
            credentials: "include",
            body: formData,
        })
            .then((raw) => raw.json())
            .then((res) => {
                if (res.success) {
                    getService(id);
                    toastType = "success";
                    toastMessage = "Review Submitted";
                    showToast = true;
                } else {
                    console.log(res.error);
                    toastType = "error";
                    toastMessage = res.error;
                    showToast = true;
                }
            });
    };

What is the expected behavior?

No response

What do you see instead?

I am getting the following error in my Hono app :

error-handler ran haiga-api-all | 1 | // src/http-exception.ts haiga-api-all | 2 | var HTTPException = class extends Error { haiga-api-all | 3 | res; haiga-api-all | 4 | status; haiga-api-all | 5 | constructor(status = 500, options) { haiga-api-all | 6 | super(options?.message, { cause: options?.cause }); haiga-api-all | ^ haiga-api-all | error: Error haiga-api-all | at new HTTPException (/app/node_modules/hono/dist/http-exception.js:6:5) haiga-api-all | at /app/node_modules/hono/dist/middleware/csrf/index.js:28:13 haiga-api-all | at csrf2 (/app/node_modules/hono/dist/middleware/csrf/index.js:23:31) haiga-api-all | at /app/node_modules/hono/dist/compose.js:30:23 haiga-api-all | at dispatch (/app/node_modules/hono/dist/compose.js:8:32) haiga-api-all | at /app/node_modules/hono/dist/hono-base.js:195:31 haiga-api-all | at /app/node_modules/hono/dist/hono-base.js:193:13 haiga-api-all |

Additional information

It looks like csrf middleware is causing the problem.

No response

yusukebe commented 2 days ago

Hi @vinay-khatri

I can't reproduce the behavior with your information. Can you share a minimal project to reproduce it?