Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctx.request.body returns undefined #91

Closed
imkimchi opened this issue Jul 18, 2017 · 3 comments
Closed

ctx.request.body returns undefined #91

imkimchi opened this issue Jul 18, 2017 · 3 comments

Comments

@imkimchi
Copy link

imkimchi commented Jul 18, 2017

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
Copy link
Author

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
Copy link
Member

tunnckoCore commented Jul 18, 2017

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' 👍

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants