Reputation: 11
So I first use requests.head() to download the header. I do some validation(check the status code, check content-type) if that is good I download the body. However I use requests.get() to that, but using .get() not only downloads the body but also the header which I just downloaded. So I don't need to download the header twice, anyway I can download the body of a get response if the header looks good?
Upvotes: 1
Views: 1477
Reputation: 148900
Ask only the header in case of a 404 or 302 is non sense. Either you get the error and the cost is the same as directly asking the body, or you get no error and need a second request for the body.
So unless the body is really large I would directly ask for the body with a single request. And if it is large, but tranferred in chunked mode, you can read the header with the first chunk and abort the transfert if it has a wrong content-type.
HTTP HEAD
request do not have common usages, and what you describe does not really looks like one. They are normally only used when you only need the header and will never use the body.
Upvotes: 0
Reputation: 1207
You can't! This is not the way HTTP works. If you want to use less traffic you are in the right track all you need is to perform the head
requests then check if you wanna get the body. If so use get
.
Downloading the HTTP headers twice will not will not cost you (a lot) in performance or traffic and it's necessary.
Upvotes: 0
Reputation: 12012
No, there's no way. HTTP has the HEAD request, which only gets the header, but there's no request to get only the body.
Don't worry too much about efficiency until you need to; the header usually isn't too big anyway. Either use requests.get()
in the first place if the body is small, or else do requests.head()
followed by requests.get()
if you need to.
Upvotes: 1