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)