I am trying to determine if the MIME type of URLs are PDF files or not, using requests
content-type
.
The below script works fine if the queried URL actually leads to a hosted PDF file. However, with the current URL below, the content-type
is detected as text/html; charset=utf-8
even though it results in forcing a web download of a PDF file (the file itself is not readable in Chrome directly from the URL).
import requests
url = 'https://derinet.vontobel.ch/api/kid?isin=DE000VS0URS6&language=en'
def getContentType(url):
r = requests.Session().head(url, allow_redirects=True)
contentType = r.headers['content-type']
print 'Content type: ' + contentType
if (contentType == 'application/pdf') | (contentType == 'application/x-pdf'):
print "content-type is PDF"
else:
print "content-type is not PDF!"
getContentType(url)
Is there any way of checking the MIME type of the actual resulting PDF download, and not the html page (blob??) that generates it?
I have set allow_redirects=True
, but it doesn't seem to matter here.
If instead of making a HEAD
request you make a GET
, you'll get the header you're looking for:
$ python
Python 3.7.2 (default, Feb 12 2019, 08:15:36)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> url = 'https://derinet.vontobel.ch/api/kid?isin=DE000VS0URS6&language=en'
>>> r = requests.get(url)
>>> r.headers
{
'Date': 'Wed, 29 May 2019 14:26:56 GMT',
'Server': 'Apache',
'Set-Cookie': 'AL_LB=$xc/70TgziJYWEd9zZwhLxXFDJHocFA0gS9gdbEQS0N0LhFme3uS; Path=/; HTTPOnly; Domain=.vontobel.ch',
'Content-Length': '51764',
'Cache-Control': 'private',
'Content-Disposition': 'attachment; filename=KID_DE000VS0URS6_en.pdf',
'X-Frame-Options': 'SAMEORIGIN',
'Pragma': 'blah',
'Vary': 'User-Agent',
'Keep-Alive': 'timeout=2, max=500',
'Connection': 'Keep-Alive',
'Content-Type': 'application/pdf',
}
The difference in the headers is something decided by the server, so I don't think there is a way to get the correct header without a GET
request.