You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* refactor: imporve the return type for shouldParseBodyAs method
* test: coverage 100%
* feat: support the already parsed body
* feat: prevent runtime problems
* feat: add patchNode option to support patching ctx.req.body
* feat: add parsedMethods option to parse only the passed ones
* feat: support extra value under the content-type + use async/await over done in testing
A body parser for koa, based on [co-body](https://github.com/tj/co-body). support `json`, `form` and `text` type body.
16
+
Koa body parsing middleware, based on [co-body](https://github.com/tj/co-body). support `json`, `form` and `text` type body.
17
+
18
+
Parse incoming request bodies in a middleware before your handlers, available under the `ctx.request.body` property.
22
19
23
-
> Notice: this module doesn't support parsing multipart format data, please use [`@koa/multer`](https://github.com/koajs/multer) to parse multipart format data.
20
+
> ⚠ Notice: **This module doesn't support parsing multipart format data**, please use [`@koa/multer`](https://github.com/koajs/multer) to parse multipart format data.
// if nothing was parsed, body will be an empty object {}
41
42
ctx.body=ctx.request.body;
@@ -44,49 +45,61 @@ app.use(async ctx => {
44
45
45
46
## Options
46
47
47
-
***enableTypes**: parser will only parse when request type hits enableTypes, support `json/form/text/xml`, default is `['json', 'form']`.
48
-
***encoding**: requested encoding. Default is `utf-8` by `co-body`.
49
-
***formLimit**: limit of the `urlencoded` body. If the body ends up being larger than this limit, a 413 error code is returned. Default is `56kb`.
50
-
***jsonLimit**: limit of the `json` body. Default is `1mb`.
51
-
***textLimit**: limit of the `text` body. Default is `1mb`.
52
-
***xmlLimit**: limit of the `xml` body. Default is `1mb`.
53
-
***strict**: when set to true, JSON parser will only accept arrays and objects. Default is `true`. See [strict mode](https://github.com/cojs/co-body#options) in `co-body`. In strict mode, `ctx.request.body` will always be an object(or array), this avoid lots of type judging. But text body will always return string type.
54
-
***detectJSON**: custom json request detect function. Default is `null`.
48
+
-**patchNode**: patch request body to Node's `ctx.req`, default is `false`.
49
+
-**enableTypes**: parser will only parse when request type hits enableTypes, support `json/form/text/xml`, default is `['json', 'form']`.
50
+
-**encoding**: requested encoding. Default is `utf-8` by `co-body`.
51
+
-**formLimit**: limit of the `urlencoded` body. If the body ends up being larger than this limit, a 413 error code is returned. Default is `56kb`.
52
+
-**jsonLimit**: limit of the `json` body. Default is `1mb`.
53
+
-**textLimit**: limit of the `text` body. Default is `1mb`.
54
+
-**xmlLimit**: limit of the `xml` body. Default is `1mb`.
55
+
-**jsonStrict**: when set to true, JSON parser will only accept arrays and objects. Default is `true`. See [strict mode](https://github.com/cojs/co-body#options) in `co-body`. In strict mode, `ctx.request.body` will always be an object(or array), this avoid lots of type judging. But text body will always return string type.
56
+
-**detectJSON**: custom json request detect function. Default is `null`.
55
57
56
58
```js
57
-
app.use(bodyParser({
58
-
detectJSON:function (ctx) {
59
-
return/\.json$/i.test(ctx.path);
60
-
}
61
-
}));
59
+
app.use(
60
+
bodyParser({
61
+
detectJSON(ctx) {
62
+
return/\.json$/i.test(ctx.path);
63
+
},
64
+
})
65
+
);
62
66
```
63
67
64
-
***extendTypes**: support extend types:
68
+
-**extendTypes**: support extend types:
65
69
66
70
```js
67
-
app.use(bodyParser({
68
-
extendTypes: {
69
-
json: ['application/x-javascript'] // will parse application/x-javascript type body as a JSON string
70
-
}
71
-
}));
71
+
app.use(
72
+
bodyParser({
73
+
extendTypes: {
74
+
// will parse application/x-javascript type body as a JSON string
75
+
json: ["application/x-javascript"],
76
+
},
77
+
})
78
+
);
72
79
```
73
80
74
-
***onerror**: support custom error handle, if `koa-bodyparser` throw an error, you can customize the response like:
81
+
-**onError**: support custom error handle, if `koa-bodyparser` throw an error, you can customize the response like:
75
82
76
83
```js
77
-
app.use(bodyParser({
78
-
onerror:function (err, ctx) {
79
-
ctx.throw(422, 'body parse error');
80
-
}
81
-
}));
84
+
app.use(
85
+
bodyParser({
86
+
onError(err, ctx) {
87
+
ctx.throw(422, "body parse error");
88
+
},
89
+
})
90
+
);
82
91
```
83
92
84
-
***disableBodyParser**: you can dynamic disable body parser by set `ctx.disableBodyParser = true`.
93
+
-**enableRawChecking**: support the already parsed body on the raw request by override and prioritize the parsed value over the sended payload. (default is `false`)
94
+
95
+
-**parsedMethods**: declares the HTTP methods where bodies will be parsed, default `['POST', 'PUT', 'PATCH']`.
96
+
97
+
-**disableBodyParser**: you can dynamic disable body parser by set `ctx.disableBodyParser = true`.
85
98
86
99
```js
87
-
app.use(async(ctx, next) => {
88
-
if (ctx.path==='/disable') ctx.disableBodyParser=true;
89
-
awaitnext();
100
+
app.use((ctx, next) => {
101
+
if (ctx.path==="/disable") ctx.disableBodyParser=true;
102
+
returnnext();
90
103
});
91
104
app.use(bodyParser());
92
105
```
@@ -98,14 +111,16 @@ You can access raw request body by `ctx.request.rawBody` after `koa-bodyparser`
98
111
1.`koa-bodyparser` parsed the request body.
99
112
2.`ctx.request.rawBody` is not present before `koa-bodyparser`.
100
113
101
-
## Koa 1 Support
114
+
## Koa v1.x.x Support
102
115
103
-
To use `koa-bodyparser` with koa@1, please use [bodyparser 2.x](https://github.com/koajs/bodyparser/tree/2.x).
116
+
To use `koa-bodyparser` with koa@1.x.x, please use [bodyparser 2.x](https://github.com/koajs/bodyparser/tree/2.x).
0 commit comments