Unable to get body in PUT request (works in POST)

Sample code:
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});

async function handleRequest(request) {
let url=new URL(request.url)
console.log(request.bodyUsed);
console.log("fooBar");
console.log(await request.text());
url.host = "XXX.m.pipedream.net";
const forwardRequest=new Request(url, request)
return fetch(forwardRequest)
}
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});

async function handleRequest(request) {
let url=new URL(request.url)
console.log(request.bodyUsed);
console.log("fooBar");
console.log(await request.text());
url.host = "XXX.m.pipedream.net";
const forwardRequest=new Request(url, request)
return fetch(forwardRequest)
}
I'm unable to identify why PUT does not give me any body while POST does work perfectly fine. For PUT request.body is always null EVEN if it contains body. Yes I know that the code above does not make much sense but it's only an example.
6 Replies
nj
njOP15mo ago
Is it some known bug / limitation of workers or what? Why does it work perfectly ok with POST but not with PUT requests?
Hello, I’m Allie!
Have you tried cloning the Request body? Since you can't pass it on to your fetch call if you already consumed it with request.text(). Here's an example that works.
nj
njOP15mo ago
Its opposite actually:) the request.text() is empty here.
Hello, I’m Allie!
Like this?
No description
Hello, I’m Allie!
I'm getting a 404, but that appears to be coming from your origin
nj
njOP15mo ago
Hmm I'll check later... Weird... Thanks Casue for me it was empty on await req.text() while it was reaching its origin fine....
Want results from more Discord servers?
Add your server