cojs / co-body

Parse request bodies with co
MIT License
325 stars 42 forks source link

Add verify option #43

Closed f48vj closed 7 years ago

f48vj commented 8 years ago

Options

verify The verify option, if supplied, is called as verify(req, str), where str is a string of the raw request body. The parsing can be aborted by throwing an error.

Example

// verify
var body = yield parse(req, {
  verify: function(req, str) {
    req.rawBody = str;
  }
});

Koa

// verify
var body = yield parse(this, {
  verify: function(req, str) {
    req.rawBody = str;
  }
});
f48vj commented 8 years ago

@fengmk2

fengmk2 commented 8 years ago

I think verify is not belong to co-body logic, it should be another middleware or plugin to do that.

yorkie commented 8 years ago

But we can only do it at this middleware if using koa-bodyParser.

f48vj commented 8 years ago

If we get raw body before body parser (whatever co-body koa-body or koa-bodyparser) in another middleware, body parser will get nothing and throw an error. we need a way to export the raw body before parse, and koa-body or koa-bodyparser are both base on co-body. so I add verify option to do this, and it provide a chance to decide parse or not parse.

haoxins commented 8 years ago

IMO, I'm +1 for verify option. But we should not repeat code in form.js, json.js and text.js

dead-horse commented 7 years ago

I think the returnRawBody option can resolve this problem. closed for now.