To make HTTP(read api) requests in Tauri app you should use @tauri-apps/api/http
module.
EXAMPLE:
npm install -D @tauri-apps/api
In tauri.conf.json file make sure you have this:
{
"tauri": {
"allowlist": {
"http": {
"all": true, // enable all http APIs. needed.
"request": true, // enables only HTTP request API. optional
"scope": ["https://localhost/*", "https://something.else/*"] // optional
}
}
}
}
More can be found in the official docs.
Disclaimer: JSON standard does not allow comments.
Next, in your sample.ts file:
import {Body, fetch, ResponseType} from "@tauri-apps/api/http"
function apiRequest(data: FormData) {
let body = Body.form(data);
let response = await fetch(API_URL, {
method: "POST",
body: body,
timeout: 30, //seconds
responseType: ResponseType.JSON,
// headers: {key: 'val'} // if needed
});
console.log(response.data);
}
This way you don't have to worry about CORS at all.
Thanks, @Vinay Rawat, & official docs.
I hope this helps someone.
access-control-allow-origin
in the request, which doesn't make sense since that header is a response header. – Sheugh