3

I'm trying to write a python script to download files from Synology NAS API

url used:

http://IP_ADDRESS:PORT/webapi/entry.cgi?api=SYNO.FileStation.Download&version=2&method=download&path=%2FPATH%2FTO%2FFILE&mode=download&sid=SID

Downloading files from the browser is Ok, so the URL with GET is working, with python I can only get a file with this content, even with status_code of request being 200 OK:

{"error":{"code":119},"success":false}

Methods tried in python:

download = requests.get(download_url, allow_redirects=True)
open('file.csv', 'wb').write(download.content)

and:

x = requests.get(download_url, stream = True)
with open("file.csv", "wb") as csv:
   for chunk in x.iter_content(chunk_size=1024):
      if chunk:
         csv.write(chunk)

without success

a4649
  • 31
  • 2

1 Answers1

1

When using requests to download file from Synology headers should include Content-Type and Content-Disposition:

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
    'Content-Type': 'application/octet-stream',
    'Content-Disposition': 'attachment'
}

and then append _sid to the URI:

/webapi/entry.cgi?api=SYNO.FileStation.Download&version=2&method=download&path=/sample.xlsx&mode=open&sid=yss5QiB17T8NI1940PEN104300&&_sid=yss5QiB17T8NI1940PEN104300

Of course, sid is variable.

Jon.H
  • 794
  • 2
  • 9
  • 23
yusp75
  • 11
  • 2