I created an API using io.js and koa.js
As a body parser middleware, I use koa-body , which in turn uses co-body .
At one of my API endpoints, I get POST requests and I need access to the raw body of the request because I need to encode it to check if the request is valid.
Is there a way to access the unprocessed request body? I tried using the raw-body middleware, but if I use it before I call koa-body , then the co-body used in koa-body is broken. If I use it after koa-body , it does not work.
app.use(function*(next){ let rawRequestBody = yield rawBody(this.req); this.rawRequestBody = rawRequestBody; yield next; });
EDIT:
I think I found a workaround, but I don't know if this is the best solution. I think @greim's answer might be the best solution to this problem.
I added the following code before using koa-body :
app.use(function *(next) { let url = this.req.url; if(this.req.method == 'POST') { let that = this; this.req.rawBody = ''; this.req.on('data', function(chunk) { that.req.rawBody += chunk; }); } yield next; });
source share