0

I'm using Django with Python 3.5 and I'm trying to download a file from a URL, and giving the filename and path where the file should be downloaded. I am using urlretrieve for this operation, but every time am getting 500 error. But when tested in Postman, the API is working fine. Can anyone help me where I'm going wrong?

headers = {'content-type': 'application/json'}
posts= {
  "filepath":filepath
}
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
url = 'http://'+API_dns+':'+API_pod+'/api/v1/download-file?filepath='+filepath+'&tid='+tid_selected+'&username='+str(request.user)+'&download_zip_file='+zip_file
password_mgr.add_password(None, url, 'admin', 'admin')
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
opener = urllib.request.build_opener(handler)
urllib.request.install_opener(opener)
urllib.request.urlretrieve(url, os.path.join(settings.STATIC_ROOT, "to_download", zip_file))
path = '/static/to_download/'+zip_file
context = {'url':path, 'status':200, 'filename':zip_file}
return JsonResponse(context)
Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
BlizZard
  • 587
  • 5
  • 22
  • 1
    Please show the full error. I'm not even clear if the error is in the urlretrieve call to get the file, or in the API making the request to Django. – Daniel Roseman Aug 15 '19 at 13:07
  • @DanielRoseman the API is returning the data correctly when called normally, but when called inside urlretrieve, its going into exception showing 500 error – BlizZard Aug 16 '19 at 07:21

0 Answers0