Download file setting the header

Asked

Viewed 71 times

0

good afternoon. How can I download a file from a site in the URL by setting the header? For example, I have a file on a site and I want to download in python, just returns me a 403 error, so how do I manage to set the header I will use? I believe that will not give error 403.

import requests

url = "http://example.com/uploads/archive.ts"

headerr = {'Host': 'example.com',
           'User-agent': 'Windows 10',
           'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
           'Accept-language': 'en-US,en; q=0.5',
           'Accept-encoding': 'gzip, deflate',
           'Connection': 'close'}
           #tenho que fazer o download do arquivo usando o cabeçalho acima. Como faço?

try:
    requisicao = requests.get(url, headers=headerr)
    print("Feito.")
except Exception as e:
    print("Erro", e)

1 answer

1

The way to pass the headers is correct. However, you are not passing any authentication headers, such as Authorization.

You need to understand how the service expects this authentication and then apply it.

  • So... Actually it is like this, when opening the site with the file in my browser, for being a file . ts, it downloads instantly. But when sending the request by setting the header, instead of downloading the file, it just shows its contents. If I do not set anything in the header, it returns me a 403. The plan was for me to make a request setting the header and be able to download, you know? Thanks for the help.

  • Maybe the authentication is via cookie and your browser is already authenticated, so you can perform the request smoothly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.