Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created March 10, 2014 23:34
Show Gist options
  • Select an option

  • Save madrobby/9476733 to your computer and use it in GitHub Desktop.

Select an option

Save madrobby/9476733 to your computer and use it in GitHub Desktop.
Download a single file from a private GitHub repo. You'll need an access token as described in this GitHub Help article: https://help.github.com/articles/creating-an-access-token-for-command-line-use
curl -H 'Authorization: token INSERTACCESSTOKENHERE' -H 'Accept: application/vnd.github.v3.raw' -O -L https://api.github.com/repos/owner/repo/contents/path
@jakeonfire
Copy link
Copy Markdown

jakeonfire commented Dec 24, 2020

when using -O to save a file with the same name, it includes request params in the file name. so i prefer setting ref via -d like this:

curl -GLOf -H "Authorization: token ${GITHUB_TOKEN?not set}" -H "Accept: application/vnd.github.v4.raw" \
  "https://api.github.com/repos/$ORG/$REPO/contents/$FILEPATH" -d ref="$REVISION"

-G causes data (specified by -d) to be added as URL request params, but not the saved filename. -f ensures if there is an issue, like 401 bad auth, the command returns an error instead of just saving the file with the error response.

@porthunt
Copy link
Copy Markdown

porthunt commented Aug 6, 2021

I've created this simple gist in case someone needs to download a json file from a private repository and use it as a dictionary: https://gist.github.com/porthunt/b994154c054deeab7ab4073273aa75bc

@W1BTR
Copy link
Copy Markdown

W1BTR commented Jun 13, 2024

I get:

curl: (6) Could not resolve host: application
{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/repos/contents#get-repository-content",
  "status": "404"
}

I'm on windows, if that makes a difference.

My command is structured as follow:
curl -H 'Fine_Grain_Auth_Token_With_Readonly_permissions' -H 'Accept: application/vnd.github.v4.raw' -L https://api.github.com/repos/MYACCOUNT/MYREPO/contents/Bin/file.txt

I am able to access the file with a url like this (when signed in)
https://raw.githubusercontent.com/MYACCOUNT/MYREPO/main/Bin/file.txt
I have also tried having the /main in the url in the command with no change.

@cristiroma
Copy link
Copy Markdown

@W1BTR You need to pay attention to the token scope, if the repository is on another organisation repo, when you generate the PAT choose that organisation instead of your own GH account.

I had the same issue and re-created the token on the correct organization. The URL is fine. Also one must use Accept: application/vnd.github.raw+json for RAW file content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment