Skip to content

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

@sourabhchotiads

Description

@sourabhchotiads

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

  • koa-body: 4.2.0
  • Koa: 2.13.4
  • Node.js: 16.13.0

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions