0

I need to download a file from an external source, I am using Basic authentication to login to the URL

import requests response = requests.get('<external url', auth=('<username>', '<password>')) data = response.json() html = data['list'][0]['attachments'][0]['url'] print (html) data = requests.get('<API URL to download the attachment>', auth=('<username>', '<password>'), stream=True) print (data.content) 

I am getting below output

<url to download the binary data> \x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\xcb\x00\x00\x1e\x00\x1e\x00\xbe\x07\x00\x00.\xcf\x05\x00\x00\x00' 

I am expecting the URL to download the word document within the same session.

4
  • So by the sounds of it the first step is fine (returns the url you want?). What you haven't done is extract the data from the second response. "data = requests.get('<API URL to download the attachment>', stream=True)" should probably be something like "data = requests.get('<API URL to download the attachment>', stream=True).text" ? Try this and edit the question if it still doesn't work Commented Dec 5, 2016 at 13:16
  • 1
    docs.python-requests.org/en/master/user/advanced/… and maybe also pass auth in the second request as well? Commented Dec 5, 2016 at 13:17
  • I recommend you re-read the Requests Quickstart documentation. To get binary data from the returned Response object you need to access its .content attribute. Commented Dec 5, 2016 at 13:23
  • @PM 2Ring - I updated the code as per your comments and I am able to download the contents of the file. The requirement is to download the document as it is (in my case its a docx file - so to be specific I need to download the docx file without any ecoding OR decoding) Commented Dec 5, 2016 at 13:59

2 Answers 2

6

Working solution

import requests import shutil response = requests.get('<url>', auth=('<username>', '<password>')) data = response.json() html = data['list'][0]['attachments'][0]['url'] print (html) data = requests.get('<url>', auth=('<username>', '<password>'), stream=True) with open("C:/myfile.docx", 'wb') as f: data.raw.decode_content = True shutil.copyfileobj(data.raw, f) 

I am able to download the file as it is.

Sign up to request clarification or add additional context in comments.

Comments

1

When you want to download a file directly you can use shutil.copyfileobj():

https://docs.python.org/2/library/shutil.html#shutil.copyfileobj

You already are passing stream=True to requests which is what you need to get a file-like object back. Just pass that as the source to copyfileobj().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.