MLhacker MLhacker - 2 months ago 10
Python Question

Python Requests vs Curl Requests on Mac Terminal to Fetch Code Content via Github API

I want to get the decrypted content of a code file in a Jquery project on Github. If I do curl request, the returned code content is decrypted.

But using the same parameter on Python requests, the encrypted exists. Why is that and what can I do to get the decrypted version?

Here's my curl command:

curl https://api.github.com/repos/jquery/jquery/git/blobs/1d2872e34a809a9469ac5cb149a40fc7b8007633 -H "Accept: application/vnd.github-blob.raw"


The output is the following:

<?php
# Load and run the test suite as a proper XHTML page
header("Content-type: application/xhtml+xml");
readfile("index.html");
?>


Here's my python code:

import requests

code = requests.get('https://api.github.com/repos/jquery/jquery/git/blobs/1d2872e34a809a9469ac5cb149a40fc7b8007633'\
,headers={'content-type':'application/vnd.github-blob.raw'})

code.json()


The output is this:

{'content': 'PD9waHAKCSMgTG9hZCBhbmQgcnVuIHRoZSB0ZXN0IHN1aXRlIGFzIGEgcHJv\ncGVyIFhIVE1MIHBhZ2UKCWhlYWRlcigiQ29udGVudC10eXBlOiBhcHBsaWNh\ndGlvbi94aHRtbCt4bWwiKTsKCXJlYWRmaWxlKCJpbmRleC5odG1sIik7Cj8+\nCg==\n',
'encoding': 'base64',
'sha': '1d2872e34a809a9469ac5cb149a40fc7b8007633',
'size': 136,
'url': 'https://api.github.com/repos/jquery/jquery/git/blobs/1d2872e34a809a9469ac5cb149a40fc7b8007633'}

Answer
>>> import base64
>>> base64.b64decode('PD9waHAKCSMgTG9hZCBhbmQgcnVuIHRoZSB0ZXN0IHN1aXRlIGFzIGEgcH
Jv\ncGVyIFhIVE1MIHBhZ2UKCWhlYWRlcigiQ29udGVudC10eXBlOiBhcHBsaWNh\ndGlvbi94aHRtbC
t4bWwiKTsKCXJlYWRmaWxlKCJpbmRleC5odG1sIik7Cj8+\nCg==')
'<?php\n\t# Load and run the test suite as a proper XHTML page\n\theader("Conten
t-type: application/xhtml+xml");\n\treadfile("index.html");\n?>\n'
>>>

alternatively send the same header that you use with the curl command...

>>> requests.get('https://api.github.com/repos/jquery/jquery/git/blobs/1d2872e34
a809a9469ac5cb149a40fc7b8007633',headers={"Accept": "application/vnd.github-blob
.raw"}).text
u'<?php\n\t# Load and run the test suite as a proper XHTML page\n\theader("Conte
nt-type: application/xhtml+xml");\n\treadfile("index.html");\n?>\n'
>>>

notice the key is "Accept" ... not "content-type"