I'm trying to send a formData object to a cloudlflare workers function. Here's the original fetch event
const response = await fetch('https://example.workers.dev/createproject', {
method: 'POST',
body: formData,
mode: 'no-cors'
if (response.ok) {
...
}
else {
...
}
}
And here is my cloudflare workers index.js
file
import { json, missing, ThrowableRouter, withParams } from 'itty-router-extras';
import { createClient } from '@supabase/supabase-js';
const router = ThrowableRouter();
// apiKey is the parsed JWT for a user.
// Not currently supported by Supabase's client, but eventually
// you'll be able to auth _as_ a user by providing this value.
const supabase = (apiKey) => createClient(SUPABASE_URL, SUPABASE_API_KEY);
const parseAuthHeader = (header) => {
if (!header)
return;
const [_, token] = header.split("Bearer ");
return token;
};
router.post('/createproject', async ({ request }) => {
try {
const formData = await request.formData();
console.log('success');
console.log(formData);
}
catch (error) {
console.log('error');
console.log(error)
}
console.log(headers);
console.log(request);
return new Response('test');
});
/*
This is the last route we define, it will match anything that hasn't hit a route we've defined
above, therefore it's useful as a 404 (and avoids us hitting worker exceptions, so make sure to include it!).
Visit any page that doesn't exist (e.g. /foobar) to see it in action.
*/
router.all("*", () => new Response("404, not found!", { status: 404 }))
addEventListener("fetch", event => {
event.respondWith(router.handle(event.request));
});
In the createproject
route, everything that's logged comes back empty (e.g. empty objects, empty arrays) — so it's clearly not accessing the request body or formData object as expected, or I'm not sending the request / formData correctly from the original fetch call.
Can someone help me fix this code so that I can access the request body and formData inside the createproject
route?