4

I'm using Python to write a manager that will download some files given some conditions. The problem is that the conditions are to be performed against the response headers.

The example below is a simplified version of what I'm doing now. I first download the file and then test whether its name, contained in the headers, is in a list defined previously.

I'd like to know if is there a way to get the response without download the content, which takes a huge time in my real case.

import requests

# The line below download the file, but I'd like not to do it.
req = requests.get('http://some_url.com/some_file')

# Get the name of the file to test if it's the right file.
r = re.search(r'filename="(.*)";', req.headers['Content-Disposition'])

filename = None

# If the filename is present in the headers...
if r.groups():
    filename = r.groups()[0]

# If the filename is in an authorized list...
if filename in [...]:
   # Process req.content
srodriguex
  • 2,900
  • 3
  • 18
  • 28
  • I disagree that this is a duplicate, the other question is asking about the html `` within the response content rather than the raw headers – Alvin May 16 '18 at 02:11
  • I also disagree that this is a duplicate. Although I can't properly post the solution here because the question is closed, the answer is within the requests docs: pass the `stream` parameter to the `get` call, which downloads the headers but defers pulling the content. https://docs.python-requests.org/en/master/user/advanced/#body-content-workflow – Daniel Jones Apr 06 '21 at 08:28

1 Answers1

16

You can use requests.head() instead of requests.get().

eddiem
  • 1,030
  • 6
  • 9
  • 3
    Caveat: this will send a `HEAD` request which is **supposed to be** identical to a `GET` request, except that no body is returned. This depends on the server handling it correctly though; it *could* return different headers for a `HEAD` request. Also, this incurs *two* HTTP requests should you decide to download the actual content with a `GET` request (but that shouldn't be a detractor). – deceze Nov 21 '16 at 15:47
  • @eddiem Is that simple? :) – srodriguex Nov 21 '16 at 15:51