Skip to content
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
12 changes: 9 additions & 3 deletions packages/next-server/server/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function apiResolver(
resolverModule: any
) {
try {
let config: PageConfig = {}
let bodyParser = true
if (!resolverModule) {
res.statusCode = 404
Expand All @@ -25,7 +26,7 @@ export async function apiResolver(
}

if (resolverModule.config) {
const config: PageConfig = resolverModule.config
config = resolverModule.config
if (config.api && config.api.bodyParser === false) {
bodyParser = false
}
Expand All @@ -36,7 +37,12 @@ export async function apiResolver(
setLazyProp({ req, params }, 'query', getQueryParser(req))
// // Parsing of body
if (bodyParser) {
req.body = await parseBody(req)
req.body = await parseBody(
req,
config.api && config.api.bodyParser && config.api.bodyParser.sizeLimit
? config.api.bodyParser.sizeLimit
: '1mb'
)
}

res.status = statusCode => sendStatusCode(res, statusCode)
Expand All @@ -59,7 +65,7 @@ export async function apiResolver(
* Parse incoming message like `json` or `urlencoded`
* @param req request object
*/
export async function parseBody(req: NextApiRequest, limit: string = '1mb') {
export async function parseBody(req: NextApiRequest, limit: string | number) {
const contentType = parse(req.headers['content-type'] || 'text/plain')
const { type, parameters } = contentType
const encoding = parameters.charset || 'utf-8'
Expand Down
6 changes: 5 additions & 1 deletion packages/next-server/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
export type PageConfig = {
amp?: boolean | 'hybrid'
api?: {
bodyParser?: boolean
/**
* The byte limit of the body. This is the number of bytes or any string
* format supported by `bytes`, for example `1000`, `'500kb'` or `'3mb'`.
*/
bodyParser?: { sizeLimit?: number | string } | false
}
experimentalPrerender?: boolean | 'inline' | 'legacy'
}
19 changes: 18 additions & 1 deletion packages/next/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ Those middlewares are:
- `req.query` - an object containing the [query string](https://en.wikipedia.org/wiki/Query_string). Defaults to `{}`
- `req.body` - an object containing the body parsed by `content-type`, or `null` if no body is sent

Body parsing is enabled by default.
Body parsing is enabled by default with a size limit of `1mb` for the parsed body.
You can opt-out of automatic body parsing if you need to consume it as a `Stream`:

```js
Expand All @@ -1124,6 +1124,23 @@ export const config = {
}
```

You can adjust size of parsed body by adding `sizeLimit` key to `bodyParser`, supported values are by [bytes](https://github.com/visionmedia/bytes.js) library.

```js
// ./pages/api/my-endpoint.js
export default (req, res) => {
// ...
}

export const config = {
api: {
bodyParser: {
sizeLimit: '1mb',
}
},
}
```

As an added bonus, you can also use any [Micro](https://github.com/zeit/micro) compatible [middleware](https://github.com/amio/awesome-micro)!

For example, [configuring CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) for your API endpoint can be done leveraging `micro-cors`.
Expand Down
11 changes: 11 additions & 0 deletions test/integration/api-support/pages/api/big-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const config = {
api: {
bodyParser: {
sizeLimit: '5mb'
}
}
}

export default (req, res) => {
res.status(200).json(req.body)
}
12 changes: 12 additions & 0 deletions test/integration/api-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ function runTests (serverless = false) {
expect(data.statusText).toEqual('Body exceeded 1mb limit')
})

it('should parse bigger body then 1mb', async () => {
const data = await fetchViaHTTP(appPort, '/api/big-parse', null, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(json)
})

expect(data.status).toEqual(200)
})

it('should parse urlencoded body', async () => {
const body = {
title: 'Nextjs',
Expand Down