Skip to content

Get pull request diff from API #24460

Answered by gr2m
n6g7 asked this question in API and Webhooks
Get pull request diff from API #24460
Feb 22, 2019 · 3 answers

Hello!

I can’t find how to programmatically retrieve an entire PR diff from the API.

I’m aware of

https://github.com/octocat/Hello-World/pull/1347.{diff,patch}

but I’m specifcally interested in private repositories, and this url doesn’t seem to accept tokens in the “Authorization” header. Using cookies is not an option in this case unfortunately.

As far as I can tell there is no way to get that from either the v3 or the v4. Am I missing something?

Thanks,

You have to pass a custom Accept header to request a different media format. 

Example

$ curl -i --header "Accept: application/vnd.github.v3.diff" https://api.github.com/repos/gr2m/sandbox/pulls/7
HTTP/1.1 200 OK
Date: Sat, 02 Mar 2019 22:28:15 GMT
Content-Type: application/vnd.github.v3.diff; charset=utf-8
Content-Length: 172
Server: GitHub.com
Status: 200 OK
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58
X-RateLimit-Reset: 1551569262
Cache-Control: public, max-age=60, s-maxage=60
Vary: Accept
ETag: "89a4add058d68717026fe382008df964"
Last-Modified: Wed, 13 Feb 2019 17:03:03 GMT
X-GitHub-Media-Type: github.v3; param=diff
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-…

Replies

3 suggested answers

I’m just going to bump this topic - I’ve been trying to figure out how to pull a PR diff from a github app, and it doesn’t seem to be possible? I would have thought looking at the contents of a PR would have been a super common use case, so I’m pretty baffled here.

0 replies

You have to pass a custom Accept header to request a different media format. 

Example

$ curl -i --header "Accept: application/vnd.github.v3.diff" https://api.github.com/repos/gr2m/sandbox/pulls/7
HTTP/1.1 200 OK
Date: Sat, 02 Mar 2019 22:28:15 GMT
Content-Type: application/vnd.github.v3.diff; charset=utf-8
Content-Length: 172
Server: GitHub.com
Status: 200 OK
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58
X-RateLimit-Reset: 1551569262
Cache-Control: public, max-age=60, s-maxage=60
Vary: Accept
ETag: "89a4add058d68717026fe382008df964"
Last-Modified: Wed, 13 Feb 2019 17:03:03 GMT
X-GitHub-Media-Type: github.v3; param=diff
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
Vary: Accept-Encoding
X-GitHub-Request-Id: D83C:7D6D:692F58:8052B4:5C7B037F

diff --git a/README.md b/README.md
index 50561f3..fcc4164 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,5 @@

sandbox

just playing around don’t mind me.
+
+test

You can request Accept: application/vnd.github.v3.patch, too.

It’s mentioned in the docs: https://developer.github.com/v3/pulls/#response-1

> Accept: application/vnd.github.v3.patch

0 replies
Answer selected

Yep, this is what I was told to do by GitHub support and that’s exactly what I needed. 👍

0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants