nj
nj
CDCloudflare Developers
Created by nj on 10/16/2024 in #workers-help
Cache API usage in worker to cache POST requests.
Hey I wanted to cache POST requests to one of my APIs and I seem to be stuck with CacheAPI. That's the code: https://anonpaste.io/share/29c8fc4d04 (sorry for link but there is char limit of 2000). I'm never getting anything out of cache, despite the cache.put being ok. As you can see that cache CHANGES the original URL so kinda tries to act like a caching proxy for POST urls. What am I doing wrong here?
6 replies
CDCloudflare Developers
Created by nj on 9/1/2023 in #workers-help
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.
9 replies