0

I use "form" to send data to my webserver. front-end:

<form action="http://localhost:3131/users" method="POST" enctype="multipart/form-data" target="_blank">
    <fieldset>
        <label for="name">Name:</label>
        <input type="text" id="name" name="name"/>
    </fieldset>
    <fieldset>
        <label for="age">Age:</label>
        <input type="text" id="age" name="age"/>
    </fieldset>
    <fieldset>
        <label for="wl">Wl:</label>
        <input type="text" id="wl" name="wl"/>
    </fieldset>
    <fieldset>
        <input type="submit" value="Submit"/>
    </fieldset>
</form>

back-end code, Github link:

var app     = require('koa')(),
router  = require('koa-router')(),
koaBody = require('koa-body')();

router.post('/users', koaBody,
  function *(next) {
    console.log(this.request.body);
    // => POST body
    this.body = JSON.stringify(this.request.body);
  }
);

app.use(router.routes());

app.listen(3131);
console.log('curl -i http://localhost:3131/users -d "name=test"');

But the result is --> this.request.body is {}.

What am i doing wrong?

Irshu
  • 8,248
  • 8
  • 53
  • 65
niutrue
  • 3
  • 2

1 Answers1

0

You are using multipart/form-data as your form data encoding, so in your server code you should pass the correct option to koa-body constructor.

kaoBody = require('koa-body)({ multipart: true });
zeronone
  • 2,912
  • 25
  • 28