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

withContent returns a 500 when no body is sent #216

Closed
smcstewart opened this issue Jan 21, 2024 · 1 comment
Closed

withContent returns a 500 when no body is sent #216

smcstewart opened this issue Jan 21, 2024 · 1 comment

Comments

@smcstewart
Copy link
Contributor

Describe the Issue

When no body is sent with a request that uses the withContent middleware, a 500 Internal Server Error is returned. The expectation is possibly for a 400 Bad Request instead, since you were expecting a body, but didn't receive one - hence bad input.

Example Router Code

Please provide the itty-router code related to the issue. If possible, create a minimal, reproducible example.

import { Router, error, json, withContent } from 'itty-router';

const router = Router();

router
	.post('/', withContent, ({ content }) => console.log(content))

	.all('*', () => error(404));

export default {
	fetch: async (request, env, ctx) =>
		router.handle(request, env, ctx).then(json).catch(error)
};

Request Details

  • Method: POST, PUT
  • URL: any / non-specific
  • Request Body: none
  • Request Headers: Content-Type: application/json

Steps to Reproduce

Steps to reproduce the behaviour:

  1. Install and run dev server (uses npm below, but use whatever package manager you like):
git clone git@github.com:smcstewart/itty-router-without-content.git
cd itty-router-without-content
npm install
npm run dev
  1. Open a new terminal and run cURL (or whatever else you like: e.g. httpie) and perhaps something like jq for a pretty-printed response:
curl -v --request POST \
  --url http://localhost:8787/ \
  --header 'Content-Type: application/json'
  1. You will see a HTTP 500 Internal Server Error and the following JSON response:
{
  "status": 500,
  "error": "Unexpected end of JSON input"
}

Expected Behaviour

I would expect to see a 400 Bad Request since the input is bad.

Actual Behaviour

It currently returns a 500 Internal Server Error.

Environment (please complete the following information):

  • Environment: Cloudflare Workers
  • itty-router Version: 4.0.27
  • Other Relevant Libraries and their versions: node 20.11.0, wrangler 3.23.0

Additional Context

None.

@kwhitley
Copy link
Owner

Closed thanks to your PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants