2

I am trying to download a binary file smaller than 1 MB through urequests with basic authentication. The authentication part works and I get the expected response.

But the truth is I feel lost since I can't download the file I need and I have to do it through urequests.

Can you give me a hand?

website = 'www.example.com/api'
username = 'test'
password = 'test'
auth_str = '%s:%s' % (username, password)
b64_auth_str = b2a_base64(auth_str)
headers = {'Authorization': 'Basic %s' % b64_auth_str.decode('utf-8')}
r = urequests.get(website,  headers=headers)

Edit:

Due to the "limitations" of the urequests library, I have tried this one: mrequests

Best library: https://github.com/SpotlightKid/mrequests

 import mrequests
 username = 'test'
 password = 'test'
 auth_str = '%s:%s' % (username, password)
 b64_auth_str = b2a_base64(auth_str)
 headers = {'Authorization': 'Basic %s' % b64_auth_str.decode('utf-8'),'accept': 'multipart/form-data'}
    url = "https://domainexample.com/file.bin"
 r = mrequests.get(url, headers=headers)
 r.save("file.bin",1024)
 r.close()

I ended up editing the Save method to handle the download percentage of the required file. It is not mandatory, but a progress bar is always healthy ;-)

dda
  • 6,030
  • 2
  • 25
  • 34

1 Answers1

0

I'm not sure if urequests can handle filles. i know json and text works great

r = urequests.get(website,  headers=headers)
data = r.text  # if text
data= r.json() # if json
r.close

# if you look at the type of data and it is a form of json response
# you will see its just a list of dict in python
print(type(ifJsonData))

However, if the binary file's have no extension (.hex), I don't think you need to do anything.

r = urequests.get(website, headers=headers)
data = r.text  # if text
r.close
print("data")

Response will be: b'I was the binary data you received'

The b means it's in binary and translated to ASCII (as far as I know).

dda
  • 6,030
  • 2
  • 25
  • 34
maximd1
  • 13
  • 6