'Using curl, how do I download a .diff file from a private repository on github

I have a private repo on github, belonging to an org. For a given pull request, there's a url to a diff file like this:

https://github.com/<ORG>/<REPO>/pull/<PR_NUMBER>.diff

But the repo is private, so how can I download this file with curl? I generated a "personal access token", but am a bit lost as to how to use it.

I tried this:

curl -H 'Authorization: token <TOKEN>' -H 'Accept: application/vnd.github.v3.raw' -O -L "https://github.com/<ORG>/<REPO>/pull/<PR_NUMBER>.diff"

But all that sent back was "Not Found".



Solution 1:[1]

The Github API returns a 404 for all unauthorized requests -- mostly to hide the existence/non-existence of user/org data at the requested URL. So the 404 you are getting back may actually be a 401.

When sending a request to the GH api via curl, you should use the u option to send your token rather than the Authorization header, resulting in the following request:

curl -u <USERNAME>:<TOKEN> \ -H 'Accept: application/vnd.github.v3.raw' -O -L \ "https://github.com/<ORG>/<REPO>/pull/<PR_NUMBER>.diff"

Solution 2:[2]

Works as of 2022. Note that the URL is the same between this and the previously-accepted answer, but the authentication method needs to change:

curl -H "Accept: application/vnd.github.v3.diff" https://<personal access token>:[email protected]/repos/<org>/<repo>/pulls/<pull request>

Also works with the normal cURL -u parameter.

See: https://docs.github.com/en/rest/reference/pulls#get-a-pull-request

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 solocommand
Solution 2 Dustin Oprea