Home > Blockchain >  Is it possible to get only the header without fetching the body using the requests.get command? the
Is it possible to get only the header without fetching the body using the requests.get command? the

Time:09-19

In a configuration I am using, a minio server hosting files, accepts only GET requests and does not accepts HEAD requests. I need the header information to check for file-type to avoid fetching the entire file.

I would do it usually with requests.head(url) however as I mentioned earlier only the GET method is allowed.

In curl it is possible to do the following:

curl -I -X GET http://domain.dom/path/

which curls the header of the url but overrides the used method with the GET HTTP method. Is there something equivalent for the Python3 requests package?

CodePudding user response:

Unfortunately there doesn't seem to be a clean way to do this. If the server accepts Range header, you could try requesting the bytes from 0 to 0, which nets you access to the header data but not the body. For example

import requests   

url = "http://stackoverflow.com"
headers = {"Range": "bytes=0-0"}
res = requests.get(url, headers=headers)

print(res.headers)

As said, this still depends on the server implementation. For reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range

CodePudding user response:

Based on the definition of a GET, it sounds like you could modify the request headers to include a range-request.

A client can alter the semantics of GET to be a "range request", requesting transfer of only some part(s) of the selected representation, by sending a Range header field in the request (Section 14.2).

I haven't tried this, but maybe setting a byte range of 0-1 would skip the body and you'd get the headers for free.

  • Related