How do I get the request as JSON?

Hello everybody.

How do I get access to the request as JSON?

I was hoping to get away with this:

export default {
  async fetch(req) {
	const j = await req.clone().json();
	return new Response(j, { status: 200, });
  },
};

but I get
SyntaxError: Unexpected end of JSON input.

I am a bit lost. What am I missing here?

Thank you for your help!

btw. I also tried
... = await req.clone().text();
There was no error, but the result was an empty string.

The request body won’t be a valid JSON object. Your test with text() proves that in particular, as that should print a JSON object.

1 Like

Thank you for following up on this so quickly.

Well … silly me!

The documentation says

json() :

  • Returns a promise that resolves with a JSON representation of the request body.

I was quite happy that the heavy lifting of putting all parameters, headers, etc. in a nice JSON had already been implemented and I totally missed the last part of the sentence :frowning:

No, I am not looking for the body … THANK YOU!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.