RknRobin RknRobin - 1 month ago 6
Python Question

How do I use python requests to download a processed files?

I'm using Django 1.8.1 with Python 3.4 and i'm trying to use requests to download a processed file. The following code works perfect for a normal request.get command to download the exact file at the server location, or unprocessed file.


The file needs to get processed based on the passed data (shown below as "data"). This data will need to get passed into the Django backend, and based off the text pass variables to run an internal program from the server and output .gcode instead .stl filetype.

python file.

import requests, os, json

SERVER='http://localhost:8000'
authuser = 'admin@google.com'
authpass = 'passwords'

#data not implimented
##############################################
data = {FirstName:Steve,Lastname:Escovar}
############################################

category = requests.get(SERVER + '/media/uploads/9128342/141303729.stl', auth=(authuser, authpass))

#download to path file
path = "/home/bradman/Downloads/requestdata/newfile.stl"
if category.status_code == 200:
with open(path, 'wb') as f:
for chunk in category:
f.write(chunk)


I'm very confused about this, but I think the best course of action is to pass the data along with request.get, and somehow make some function to grab them inside my views.py for Django. Anyone have any ideas?

Answer

To use data in request you can do

get( ... , params=data) 

(and you get data as parameters in url)

or

post( ... , data=data). 

(and you send data in body - like HTML form)

BTW. some APIs need params= and data= in one request of GET or POST to send all needed information.

Read requests documentation