Cloudflare Worker TypeError: One-time-us...

Hello, I'm trying to build a reverse proxy where I'll use the cloudflare domain to proxy from a site. Now the application sends 302 when the user is signed out, and then I get to send a post request with the login information. The code below works, in the sense that it follows the redirects, but when I submit the form I get the following error:
A request with a one-time-use body (it was initialized from a stream, not a buffer) encountered a redirect requiring the body to be retransmitted. To avoid this error in the future, construct this request from a buffer-like body initializer.
A request with a one-time-use body (it was initialized from a stream, not a buffer) encountered a redirect requiring the body to be retransmitted. To avoid this error in the future, construct this request from a buffer-like body initializer.
I checked, https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body and started buffering the body if it's present but I still get the same error, here is my code:
export default {
async fetch(request, env, ctx) {
// If body present buffer it.
let body;
if (request.body) {
body = await request.arrayBuffer();
}

let req = new Request(request, {
redirect: 'follow',
body: body,
});
console.log(req.body);

const url = new URL(request.url);
url.hostname = 'gdk.test';
url.port = '3000';

return await fetch(url.toString(), req);
},
};
export default {
async fetch(request, env, ctx) {
// If body present buffer it.
let body;
if (request.body) {
body = await request.arrayBuffer();
}

let req = new Request(request, {
redirect: 'follow',
body: body,
});
console.log(req.body);

const url = new URL(request.url);
url.hostname = 'gdk.test';
url.port = '3000';

return await fetch(url.toString(), req);
},
};
The console.log(req.body) output still shows a ReadableStream
ReadableStream {
locked: false,
[state]: 'readable',
[supportsBYOB]: true,
[length]: 211n
}
ReadableStream {
locked: false,
[state]: 'readable',
[supportsBYOB]: true,
[length]: 211n
}
How can I fix this error?
Stack Overflow
Cloudflare Worker TypeError: One-time-use body
I'm trying to use a Cloudflare Worker to proxy a POST request to another server. It is throwing a JS exception – by wrapping in a try/catch blog I've established that the error is: TypeError: A
2 Replies
Steve Xuereb
Steve XuerebOP11mo ago
What is strange is that the backend server (gdk.test:3000 ) is getting the POST request just fine The backend server is responding with a 302 for a redirect
Steve Xuereb
Steve XuerebOP11mo ago
GitLab
fix(router): handle redirects (a51e9eff) · Commits · GitLab.org / T...
What --- Handle redirects for things like loging in, POST requests Why --- When we send a success POST /user/sign_in is sent we get a 302, with a header `location:...
Want results from more Discord servers?
Add your server