helapkg / hela

:icecream: Powerful software development experience and management. Enhancing @tc39 JS, @denoland and @nodejs, because we need a bit of magic. :sparkles: You can think of it as Cargo for the JavaScript ecosystem.
Mozilla Public License 2.0
331 stars 41 forks source link

ctx.request.body returns undefined #91

Closed imkimchi closed 7 years ago

imkimchi commented 7 years ago

after multipart/form-data parsing works fine, there are some issues with other routers that don't use multipart/form-data, but json request. It sends POST request from client using axios. and It worked fine with koa-bodyparser but doesn't work with koa-better-body.

I've tested some codes and I figured that It parse the POST request but not body. I read that koa-better-body will parse no body when the header is multipart/form-data but the POST request is just a json request.

request info image

client code

        let payload // <-- object for request

        let option = {
            method: "POST",
            url: '/post/upload',
            data: JSON.stringify(payload)
        }
        try {
            await axios(option)
        } catch (e) {
            console.error("failed to send signup request", e)
        }

router

router.post("/post/upload", async (ctx, next) => {
  let data = ctx.request.body; // <-- undefined
  const param = await makeParam(data);

  try {
    const post = new Post(param);
    await post.save();
    ctx.redirect("/");
  } catch (e) {
    console.error("Failed to save post request", e, param);
  }
});

app.js

const app = new Koa();
const port = process.env.PORT || 3000;
const dist = __dirname + "/views/";
const bpOption = { IncomingForm: form };

app.keys = ["secret", "key"];
require("./util/passport");

app
  .use(logger())
  .use(serve(dist))
  .use(session({}, app))
  .use(bodyParser(bpOption)) // koa-better-body
  .use(passport.initialize())
  .use(passport.session())
  .use(views(__dirname + "/views", { extension: "pug" }))
  .use(routes());

app.listen(port, () => console.log(`[!] Server is Running on ${port}`));
imkimchi commented 7 years ago

I just figured out It stored in ctx.request.fields not ctx.request.fields :( It needs to be fixed as koa-bodyparser store everything in body to ctx.request.body and people could get confused easily

tunnckoCore commented 7 years ago

Oooooh damn, i forgot that. We had issues with that before. Before few versions it was body. i believe. But was changed if i remember correctly. However, it can be changed with options, so not so big problem :) Maybe some notes need, but i always think that this module si mostly the best documented of my all. Don't know.

Try options.fields to set it to string options.fields: 'body' :+1:

edit: .request.fields is just preference. It make a lot of sense, exactly in json context, because any sane parser should handle JSON by default. So we do that: response comes -> if JSON -> parse it -> push it as fields. Probably, don't know.

edit2: It writes to ctx.request.body only when buffer and text.

tunnckoCore commented 7 years ago

Anyway, very thanks that we clarified all that mess :)