# Problema CORS nel postare su Discourse da Obsidian

**URL:** <https://meta.discourse.org/t/cors-issue-posting-to-discourse-from-obsidian/310636>\
**Category:** Development\
**Created:** [5 Giugno 2024, 10:03am UTC](https://meta.discourse.org/t/cors-issue-posting-to-discourse-from-obsidian/310636 "2024-06-05T10:03:03Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [5 Giugno 2024, 10:03am UTC](https://meta.discourse.org/t/cors-issue-posting-to-discourse-from-obsidian/310636/1 "2024-06-05T10:03:03Z")

</div>

Ho iniziato a usare [Obsidian](https://obsidian.md/) per scrivere, quindi ho pensato di creare un [plugin Obsidian](https://docs.obsidian.md/Home) per pubblicare sul mio forum Discourse. In teoria è facile da fare, ma la risposta del mio primo tentativo è stata:

`Access to fetch at 'http://localhost:4200/posts.json' from origin 'app://obsidian.md' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.`

La richiesta viene effettuata dal client, ma il client è il dispositivo dell’utente. Se ho capito bene, questo è sicuro:

```ts
import DiscoursePlugin from "./main";
import { TFile } from "obsidian";

export async function publishToDiscourse(
	plugin: DiscoursePlugin,
	activeFile: TFile
): Promise<{ message: string }> {
	try {
		const content = await plugin.app.vault.read(activeFile);
		const baseUrl = plugin.settings.baseUrl;
		const apiKey = plugin.settings.apiKey;

		const headers = new Headers({
			"Content-Type": "application/json",
			"Api-Key": apiKey,
			"Api-Username": "scossar", // this needs to be a setting
		});
		const body = JSON.stringify({
			title: activeFile.name,
			raw: content, // TODO: parse the content to fix internal links
			category: 1, // this needs to be a setting
		});
		// TODO: check to see if the note has already been published
		const url = `${baseUrl}/posts.json`;

		const response = await fetch(url, { method: "POST", headers, body });

		if (!response.ok) {
			console.error("Error publishing to Discourse:", response.status);
			return { message: "Error publishing to Discourse" };
		}
        
		const jsonResponse = await response.json();
		// TODO: use the response to add a discoursePostId file property
		console.log(`jsonResponse: ${JSON.stringify(jsonResponse, null, 2)}`);
		return { message: "success" };
	} catch (error) {
		console.error("Error publishing to Discourse:", error);
		return { message: `Error: ${error.message}` };
	}
}

```

È possibile che ci sia una soluzione ovvia che mi sfugge. In caso contrario, c’è un modo in cui Discourse potrebbe permetterlo? Un plugin Discourse per Obsidian sembra che potrebbe essere utile. (Un’implementazione corretta sarebbe più complessa di quella che ho postato sopra.)

---

_[View the full topic](https://meta.discourse.org/t/cors-issue-posting-to-discourse-from-obsidian/310636)._
