-
Notifications
You must be signed in to change notification settings - Fork 65.4k
Description
Code of Conduct
- I have read and agree to the GitHub Docs project's Code of Conduct
What article on docs.github.com is affected?
Repository contents
Using CORS and JSONP to make cross-origin requests
What part(s) of the article would you like to see updated?
The Download a repository archive (zip) show how to build the request to download a repository as a zip archive.
However, with all my attempts, I wasn't able to make it work in a way that I could retrieve the zip file and process it, it was only possible to make the download start in the browser.
In the About cross-origin requests section it says:
The REST API supports cross-origin resource sharing (CORS) for AJAX requests from any origin.
However I can't seem to make it work, even trying with JSON-P callbacks as described in the linked section.
Additional information
I'm trying to build an Angular application hosted on my GitHub pages with a custom verified domain.
This is my code to call GitHub API and get the zip archive of a (public) repository of mine:
public getTemplate() {
return this.http.get(
'https://api.github.com/repos/crystal-nest/cobweb-mod-template/zipball/1.20.4',
{
headers: {
'X-GitHub-Api-Version': '2022-11-28',
'Accept': 'application/vnd.github.raw+json'
}
}
);
}I know the URL is correct because if I paste it in my browser the download starts.
However, when the code gets executed I get the following error:
Access to XMLHttpRequest at 'https://codeload.github.com/Crystal-Nest/cobweb-mod-template/legacy.zip/refs/heads/1.20.4' (redirected from 'https://api.github.com/repos/crystal-nest/cobweb-mod-template/zipball/1.20.4') from origin 'https://crystalnest.it' 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.
I then tried with JSONP requests:
return this.http.jsonp('https://api.github.com/repos/crystal-nest/cobweb-mod-template/zipball/1.20.4', 'callback');But this time the error I received was:
{
"headers": {
"normalizedNames": {},
"lazyUpdate": null,
"headers": {}
},
"status": 0,
"statusText": "JSONP Error",
"url": "https://api.github.com/repos/crystal-nest/cobweb-mod-template/zipball/1.20.4?callback=ng_jsonp_callback_0",
"ok": false,
"name": "HttpErrorResponse",
"message": "Http failure response for https://api.github.com/repos/crystal-nest/cobweb-mod-template/zipball/1.20.4?callback=ng_jsonp_callback_0: 0 JSONP Error",
"error": {
"message": "JSONP injected script did not invoke callback.",
"stack": "Error: JSONP injected script did not invoke callback.\n stacktrace omitted for the sake of brevity"
}
}My code can be found here, and here there's a live example (click the download button and ignore everything else, the website is very WIP at the moment).
Side note: even if the request worked and I was able to get the file as an 'arraybuffer' (the data I need to process the file with JSZip), it's a pain that the zip name and its root directory have a code appended at the end (that I can't understand what it is and thus I can't predict what it's going to be).
Ideally, I'd like to use this URL https://github.com/crystal-nest/cobweb-mod-template/archive/refs/heads/1.20.4.zip because it gives me a name easy to predict and thus easy to process.
But it'd be enough to just be able to predict the name so that I can handle it properly.