Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cors Error on Post Request to APi #121

Open
AbdulSami969 opened this issue Feb 16, 2024 · 4 comments
Open

Cors Error on Post Request to APi #121

AbdulSami969 opened this issue Feb 16, 2024 · 4 comments

Comments

@AbdulSami969
Copy link

WHen i do post request from localhost it gives cors error how to fix it?

@wenbinf
Copy link
Member

wenbinf commented Feb 17, 2024

Thanks for asking, @AbdulSami969

Could you please share specifics of your issue? This can greatly help us understand the issue you encountered. Otherwise, it's really difficulty to guess what went wrong by "WHen i do post request from localhost it gives cors error how to fix it?"

What post requests? How do you send post requests? From your custom code? If so, could you share the code snippet here?

@AbdulSami969
Copy link
Author

AbdulSami969 commented Feb 17, 2024

Thanks for replying!
This is the code I am trying to post data with this code. Its working perfect on postman but when i try to send it from localhost next.js it gives cors error in console!

`
const handleClick = async () => {
const url = "https://downlight-microfeed.pages.dev/api/items/";
const apiKey = "hidden";
const requestBody = {
title: "Episode 1 - Introduction",
status: "published",
attachment: {
category: "audio",
url: "",
},
url: "https://example.com/episode1",
content_html: "some text",
image: "",
date_published_ms: 1669782691867,
_microfeed: {
"itunes:title": "Another episode title",
"itunes:block": false,
"itunes:episodeType": "full",
"itunes:season": 1,
"itunes:episode": 2,
"itunes:explicit": false,
},
};

	try {
		const response = await fetch(url, {
			method: "POST",
			header: {
				"Content-Type": "application/json",
				"X-MicrofeedAPI-Key": apiKey,
			},
			body: JSON.stringify(requestBody),
		});
		if (response.ok) {
			console.log("Request successful");
			// Handle the response data if needed
		} else {
			console.error("Request failed with status:", response.status);
			// Handle the error response if needed
		}
	} catch (error) {
		console.log("Error:", error.message);
	}
};`
`

@wenbinf
Copy link
Member

wenbinf commented Feb 17, 2024

It seems to me that you are trying to send api requests from client-side javascript. It's generally not a good practice to send APi requests from client-side, as you will leak api key to the public in browsers.

Could you try sending requests using server-side javascript?

It could be helpful to learn more about CORS, as it could help you further troubleshoot & architect your app: https://www.google.com/search?q=cors

@prabha-ar
Copy link

Hi @wenbinf I am building a citizen run NEWS app .. using MICROFEED as CMS thorough REST API

How does this REST CRUD operations work, does it uses cloudflare workers?
will it handle a large write operations at a time, is there any limit ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants