You can’t JSON.stringify() the entire request object like that - it’s not all serializable.
If you’re submitting a POST request, you probably want to read the body first, like this:
export default {
async fetch(request, env) {
const body = await request.json();
return Response.json(body); // same as JSON.stringify + JSON content type headers
}
}
There’s no error handling there and in production you’d probably want to check if it’s a POST request, check for invalid JSON, etc. but it’ll work as long as the JSON you post is valid.
You likely have invalid JSON. You should see the errors in the console when testing in the playground, otherwise I would recommend wrangler for better errors and debugging:
This works for me though:
export default {
async fetch(request, env) {
if (request.method === "POST") {
// Getting the POST request JSON payload
const payload = await request.json()
return Response.json(payload)
}
return new Response('get');
}
}