Skip to content

Commit

Permalink
Merge pull request #74 from domwebber/master
Browse files Browse the repository at this point in the history
Add Error Handling for JSON body parsing
  • Loading branch information
mendhak authored Oct 19, 2024
2 parents e541281 + 33314c1 commit 8fb4ccd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ app.all('*', (req, res) => {

//If the Content-Type of the incoming body `is` JSON, it can be parsed and returned in the body
if(req.is('application/json')){
echo.json = JSON.parse(req.body)
try {
echo.json = JSON.parse(req.body)
} catch (error) {
console.warn("Invalid JSON Body received with Content-Type: application/json", error);
}
}

//If there's a JWT header, parse it and decode and put it in the response
Expand Down
9 changes: 9 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ else
exit 1
fi

REQUEST=$(curl -s -X POST -H "Content-Type: application/json" -d 'not-json' http://localhost:8080)
if [ $(echo $REQUEST | jq -r '.json') == 'null' ]; then
passed "JSON with Invalid Body test passed."
else
failed "JSON with Invalid Body test failed."
echo $REQUEST | jq
exit 1
fi

message " Stop containers "
docker stop http-echo-tests
sleep 5
Expand Down

0 comments on commit 8fb4ccd

Please sign in to comment.