What is the use of http headers?

  • Thread starter Thread starter adjacent
  • Start date Start date
AI Thread Summary
When downloading files using a Python script, including HTTP headers can be important for proper communication with the server. While the only mandatory header in HTTP 1.1 is "Host:", sending additional headers can help define content encoding and manage TCP connections. Headers may also be necessary for authentication, depending on the website's requirements. Some sites may use headers to identify bots, but this is uncommon and typically applies to copyrighted material. If issues arise, tools like netcat or packet capture software can help analyze the headers sent by browsers. Major browsers also provide debugging tools, accessible via the F12 key, to inspect request and response headers.
adjacent
Gold Member
Messages
1,552
Reaction score
62
For example, I am downloading an image file from example.com using a python script.
Is there any need to include the headers in the script? I don't see anything happening if I don't include the headers.
So what is the need of it?
 
Computer science news on Phys.org
As far as I'm aware of the only mandatory header in HTTP 1.1 is "Host: ". However it might be better if your script can also send appropriate headers to define the encoding, and request the TCP connection to be closed or kept open.
 
  • Like
Likes 1 person
[Edit: I thought you were only trying to download an image :-p ]

You need http header options especially in case the website in contact needs e.g your credentials for access (username, password, apikey etc). Plus, it depends on which method you use to send your packet so as to include your appropriate request header i.e partial vs full download stream, request string from your client to the server in transaction etc.

If for example you would want to download an image from the site example.com, you can just need to GET the image location provided that you've been granted your access right to the site.
In case you would want to query something from the site which has supplied you with i.e public api methods, they sure also document them on their website, visit and follow their examples.
 
Last edited:
  • Like
Likes 1 person
Thanks for the answers. Can the site Identify me as a bot if I don't use the headers?
 
Can the site Identify me as a bot if I don't use the headers?

Some sites do use headers for that, but it's very few of them and in most cases that's just for protecting copyrighted material (e.g. videos). So most likely you won't have trouble.

If you notice that it doesn't work (and you're not doing any nasty things) I'd suggest you use netcat in server mode and send a browser request to it. You'll be able to see on screen all the headers that your browser sends. Alternatively, you can use packet capture software to inspect your browser's requests to the real site.
 
  • Like
Likes 1 person
martiandawn said:
If you notice that it doesn't work (and you're not doing any nasty things) I'd suggest you use netcat in server mode and send a browser request to it. You'll be able to see on screen all the headers that your browser sends. Alternatively, you can use packet capture software to inspect your browser's requests to the real site.

The 'firebug' add-on for firefox can also be used to find the request and response headers. :smile:
 
The 'firebug' add-on for firefox can also be used to find the request and response headers.

Thanks for the tip!
 
All of the major browsers (Firefox, Chrome, IE) have debugging capabilities that can be accessed from the F12 key. Using the F12 debugging tools you can see the request and response headers and quite a bit more.
 
  • Like
Likes 2 people
  • #10
Mark44 said:
All of the major browsers (Firefox, Chrome, IE) have debugging capabilities that can be accessed from the F12 key. Using the F12 debugging tools you can see the request and response headers and quite a bit more.

I've played with web apps for years and never knew about the F12 key. Thx.
 
Back
Top