CORS blocking on Github Pages even with headers set

I’m trying to host a demo on Github Pages but when loading the data from the Horizons API I get CORS errors. You can see them over at Unity WebGL Player | Solardemo. I followed the instructions from Unity - Manual: WebGL Networking which led me to using this code:

UnityWebRequest webRequest = UnityWebRequest.Get(…)

webRequest.SetRequestHeader("Access-Control-Allow-Credentials", "true");
webRequest.SetRequestHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
webRequest.SetRequestHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
webRequest.SetRequestHeader("Access-Control-Allow-Origin", "*");

using (webRequest)
        {
            yield return webRequest.SendWebRequest();
            // …
        }

However, I’m still receiving the same errors. What can I do to resolve this?

i thought in part it was actually the remote end that had to enable cors

I thought the same, when researching online I find that Github should just allow it… (The page is public)

Pages allows CORS (access-control-allow-origin header is set to *) for public sites only

right but your remote is https://ssd.jpl.nasa.gov/api …so github isnt the thing in question… interestingly the site lists a url other than the one you use as the api for horizons…

Is there any way I can work around this issue? There are a lot of things online about proxies, but I find them pretty confusing…

im not sure what you find confusing but, i googled cors proxy, i found 10 free… so i picked the first, and followed exactly what it said, and lo it works

When I tried using https://corsproxy.io/ I got a 403 forbidden error back.

I sent one of your urls down it and it sent me back a massive pile of data… so Im guessing you forgot the ?

I shouldnt have to code it for you but

given a url i stole out your errors of “https://ssd.jpl.nasa.gov/api/horizons.api?format=text&COMMAND=299&OBJ_DATA=YES&MAKE_EPHEM=YES&EPHEM_TYPE=VECTOR&START_TIME=2024-04-18&STOP_TIME=2024-04-19&CENTER=500@10

UnityWebRequest.Get("https://corsproxy.io/?"+ System.Web.HttpUtility.HtmlDecode(uri))

works just fine…

1 Like

After actually building and deploying the site it works! For some reason when I try it in unity it blocks me, but it doesn’t error in Github Pages and that was the goal anyway. Thanks so much for the help and your persistence :slight_smile:

It should work either way. Did for me.