koajs / koa-body

koa body parser middleware
MIT License
948 stars 131 forks source link

Not able to parse multipart form data using new FormData #203

Open sourabhchotiads opened 2 years ago

sourabhchotiads commented 2 years ago

Hello,

I am unable to parse the multipart form data sent using new FormData from frond end, it works fine in the postman form data. I am new to koa, can anyone help me here

Environment Information

Current Behavior

i am using below code to pass the multipart option to middleware

`const KoaRouter = require("@koa/router");
const koaBody = require("koa-body");
const Api = require("./apis/apis");

const app = new KoaRouter({ prefix: "/" });

app.post(
  "query-update-profile-picture",
  koaBody({
    multipart: true,
    formLimit: 1024 * 1024,
    formidable: {
      maxFileSize: 1024 * 1024
    }
  }),
  Api.updateProfilePicture
);`

My server.js is

`const Koa = require("koa");
const routes = require("./routes");
const app = new Koa();

app.use(routes.routes());`

and in my upload function, if a console

`console.log(ctx.request.files);
 console.log(ctx.request.body);`

it gives me below response for body, file is undefined....

it is not parsing the request body
image

My front end handler function is

`const imageData = new FormData();
 imageData.append("profile_picture", target.files[0]);
 imageData.append("id", id);

await updateProfileImage(imageData);`

UpdateProfileImage function

`const response = await axios.post(
        url,
        body
      );`
  Can any one help me?
guillenotfound commented 1 year ago

Same here, any updates?