Purpur
Purpur
CDCloudflare Developers
Created by Purpur on 3/1/2024 in #general-help
[Resolved] Unable to POST on Cloudflare Pages
Hello, I've been trying to fix my code to get it to work for a few hours, but have been quite unsuccessful. Running a CF Pages site that is supposed to POST (server-side) to an external API. My code runs fine locally, but it doesn't POST on CF. I can't figure out why. Would appreciate any help. Does CF Pages not support server-side POST requests? I use a lot of GET requests so I'd be surprised if it doesn't support POST requests... Here is the relevant code:
export function collector(id: string, url: string, type: 'spark' | 'timings') {
if (!url) return;
return fetch(url + '/' + type, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id }),
}).catch(error => {
console.error('Fetch error:', error);
return Promise.reject(error);
});
}

export const useResults = routeLoader$(async ({ params }) => {
try {
await collector(params.id, 'https://api.url.com', 'spark');
} catch (error) {
console.error('Collector error:', error);
}
return await analyzeProfile(params.id);
});

export default component$(() => {
const results = useResults();
return(...);
}
export function collector(id: string, url: string, type: 'spark' | 'timings') {
if (!url) return;
return fetch(url + '/' + type, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id }),
}).catch(error => {
console.error('Fetch error:', error);
return Promise.reject(error);
});
}

export const useResults = routeLoader$(async ({ params }) => {
try {
await collector(params.id, 'https://api.url.com', 'spark');
} catch (error) {
console.error('Collector error:', error);
}
return await analyzeProfile(params.id);
});

export default component$(() => {
const results = useResults();
return(...);
}
edit: not exactly sure what I changed, but works now. Had something to do with viewing an outdated file that didn't actually exist instead of the real file.
1 replies