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

feat: ctx.request.rawBody to get raw request body #70

Merged
merged 1 commit into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ app.use(async (ctx, next) => {
app.use(bodyparser());
```

## Raw Body

You can access raw request body by `ctx.request.rawBody` after `koa-bodyparser` when:

1. `koa-bodyparser` parsed the request body.
2. `ctx.request.rawBody` is not present before `koa-bodyparser`.

## Koa 1 Support

To use `koa-bodyparser` with koa@1, please use [bodyparser 2.x](https://github.com/koajs/bodyparser/tree/2.x).
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ module.exports = function (opts) {
opts.detectJSON = undefined;
opts.onerror = undefined;

// force co-body return raw body
opts.returnRawBody = true;

// default json types
var jsonTypes = [
'application/json',
Expand Down Expand Up @@ -70,7 +73,9 @@ module.exports = function (opts) {
if (ctx.request.body !== undefined) return await next();
if (ctx.disableBodyParser) return await next();
try {
ctx.request.body = await parseBody(ctx);
const res = await parseBody(ctx);
ctx.request.body = 'parsed' in res ? res.parsed : {};
if (ctx.request.rawBody === undefined) ctx.request.rawBody = res.raw;
} catch (err) {
if (onerror) {
onerror(err, ctx);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"supertest": "^3.0.0"
},
"dependencies": {
"co-body": "^5.0.0",
"co-body": "^5.1.0",
"copy-to": "^2.0.1"
},
"engines": {
Expand Down
15 changes: 14 additions & 1 deletion test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ var fixtures = path.join(__dirname, 'fixtures');

describe('test/middleware.test.js', function () {
describe('json body', function () {
var app = App();
var app;
beforeEach(function() {
app = App();
});

it('should parse json body ok', function (done) {
// should work when use body parser again
app.use(bodyParser());

app.use(async (ctx) => {
ctx.request.body.should.eql( {foo: 'bar'} );
ctx.request.rawBody.should.equal('{"foo":"bar"}');
ctx.body = ctx.request.body;
});
request(app.listen())
Expand All @@ -47,6 +51,7 @@ describe('test/middleware.test.js', function () {

app.use(async (ctx) => {
ctx.request.body.should.eql( {foo: 'bar'} );
ctx.request.rawBody.should.equal('{"foo": "bar"}');
ctx.body = ctx.request.body;
});
request(app.listen())
Expand All @@ -61,6 +66,7 @@ describe('test/middleware.test.js', function () {
var app = App();
app.use(async (ctx) => {
ctx.request.body.should.eql( [{op: 'add', path: '/foo', value: 'bar'}] );
ctx.request.rawBody.should.equal('[{"op": "add", "path": "/foo", "value": "bar"}]');
ctx.body = ctx.request.body;
});
request(app.listen())
Expand All @@ -84,6 +90,7 @@ describe('test/middleware.test.js', function () {
it('should json body error with string in strict mode', function (done) {
var app = App({jsonLimit: 100});
app.use(async (ctx) => {
ctx.request.rawBody.should.equal('"invalid"');
ctx.body = ctx.request.body;
});
request(app.listen())
Expand All @@ -96,6 +103,7 @@ describe('test/middleware.test.js', function () {
it('should json body ok with string not in strict mode', function (done) {
var app = App({jsonLimit: 100, strict: false});
app.use(async (ctx) => {
ctx.request.rawBody.should.equal('"valid"');
ctx.body = ctx.request.body;
});
request(app.listen())
Expand All @@ -116,6 +124,7 @@ describe('test/middleware.test.js', function () {

app.use(async (ctx) => {
ctx.request.body.should.eql( {foo: 'bar'} );
ctx.request.rawBody.should.equal('{"foo":"bar"}');
ctx.body = ctx.request.body;
});

Expand All @@ -133,6 +142,7 @@ describe('test/middleware.test.js', function () {
});

app.use(async (ctx) => {
ctx.request.rawBody.should.equal('{"foo":"bar"}');
ctx.body = ctx.request.body;
});

Expand All @@ -150,6 +160,7 @@ describe('test/middleware.test.js', function () {
it('should parse form body ok', function (done) {
app.use(async (ctx) => {
ctx.request.body.should.eql( { foo: {bar: 'baz'} } );
ctx.request.rawBody.should.equal('foo%5Bbar%5D=baz');
ctx.body = ctx.request.body;
});
request(app.listen())
Expand All @@ -176,6 +187,7 @@ describe('test/middleware.test.js', function () {
});
app.use(async (ctx) => {
ctx.request.body.should.equal('body');
ctx.request.rawBody.should.equal('body');
ctx.body = ctx.request.body;
});
request(app.listen())
Expand Down Expand Up @@ -293,6 +305,7 @@ describe('test/middleware.test.js', function () {
});
app.use(bodyParser());
app.use(async (ctx) => {
(undefined === ctx.request.rawBody).should.equal(true);
ctx.body = ctx.request.body ? 'parsed' : 'empty';
});
request(app.listen())
Expand Down