Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
ec440d4b
Unverified
Commit
ec440d4b
authored
Dec 26, 2019
by
Joe Haddad
Committed by
GitHub
Dec 26, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow Empty API Body (#9838)
parent
3013c539
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
0 deletions
+16
-0
api-utils.ts
packages/next/next-server/server/api-utils.ts
+5
-0
index.test.js
test/integration/api-support/test/index.test.js
+11
-0
No files found.
packages/next/next-server/server/api-utils.ts
View file @
ec440d4b
...
@@ -104,6 +104,11 @@ export async function parseBody(req: NextApiRequest, limit: string | number) {
...
@@ -104,6 +104,11 @@ export async function parseBody(req: NextApiRequest, limit: string | number) {
* @param str `JSON` string
* @param str `JSON` string
*/
*/
function
parseJson
(
str
:
string
)
{
function
parseJson
(
str
:
string
)
{
if
(
str
.
length
===
0
)
{
// special-case empty json body, as it's a common client-side mistake
return
{}
}
try
{
try
{
return
JSON
.
parse
(
str
)
return
JSON
.
parse
(
str
)
}
catch
(
e
)
{
}
catch
(
e
)
{
...
...
test/integration/api-support/test/index.test.js
View file @
ec440d4b
...
@@ -75,6 +75,17 @@ function runTests(dev = false) {
...
@@ -75,6 +75,17 @@ function runTests(dev = false) {
expect
(
data
).
toEqual
([{
title
:
'Nextjs'
}])
expect
(
data
).
toEqual
([{
title
:
'Nextjs'
}])
})
})
it
(
'should special-case empty JSON body'
,
async
()
=>
{
const
data
=
await
fetchViaHTTP
(
appPort
,
'/api/parse'
,
null
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json; charset=utf-8'
,
},
}).
then
(
res
=>
res
.
ok
&&
res
.
json
())
expect
(
data
).
toEqual
({})
})
it
(
'should return error with invalid JSON'
,
async
()
=>
{
it
(
'should return error with invalid JSON'
,
async
()
=>
{
const
data
=
await
fetchViaHTTP
(
appPort
,
'/api/parse'
,
null
,
{
const
data
=
await
fetchViaHTTP
(
appPort
,
'/api/parse'
,
null
,
{
method
:
'POST'
,
method
:
'POST'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment