2017-09-05 2 views
0

Ich arbeite an einem Projekt, bei dem mehrere Formularparameter angegeben werden müssen, von denen eines die Datei selbst ist.Wie senden Sie mehrere Formularparameter in Anfrage?

Was ich versucht:

import requests 
REST_URL = 'http://192.168.150.138:8888/tasks/create/file' 
with open(os.path.join('/home/default/Batch/Samples/', filename),'rb') as sample: 
      files = {'file' :("temp_file_name" , sample)} 
      r = requests.post(REST_URL , files=files) 

Problem:

ich die zusätzlichen Informationen wie diese passieren müssen (Alle diese sind Formparameter)

file (required) - sample file (multipart encoded file content) 
package (optional) - analysis package to be used for the analysis 
timeout (optional) (int) - analysis timeout (in seconds) 
priority (optional) (int) - priority to assign to the task (1-3) 
options (optional) - options to pass to the analysis package 
machine (optional) - label of the analysis machine to use for the analysis 
platform (optional) - name of the platform to select the analysis machine from (e.g. “windows”) 

Angenommen, wenn Ich möchte Maschinennamen auch in der Form senden kann ich so erstellen?

data = {'machine' :'machine_name'} 
r =requests.post(EST_URL , files=files,data=data) 

Alle Vorschläge helfen.

+0

Ja, Importanforderungen –

+0

@ die-g Typ korrigiert –

Antwort

0

Frage: Nehmen wir an, wenn ich will auch ich kann wie folgt erstellen in Form Computernamen schicken?


Anfrage Kurz More complicated POST requests

Wenn Sie requests Parameter testen möchten, können Sie das folgende laufen:

import requests, io 
url = 'http://httpbin.org/anything' 

sample = io.StringIO('lorem ipsum') 
files = {'file': ("temp_file_name", sample)} 
data = {'machine': 'machine_name'} 
r = requests.post(url, data=data, files=files) 

r_dict = r.json() 
for key in r_dict: 
    print('{}:{}'.format(key, r_dict[key])) 

Ausgang:

json:None 
headers:{'Connection': 'close', 'Content-Length': '261', 'User-Agent': 'python-requests/2.11.1', 'Content-Type': 'multipart/form-data; boundary=5ed95afb5ea2437eade92a826b29be0d', 'Host': 'httpbin.org', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*'} 
data: 
args:{} 
files:{'file': 'lorem ipsum'} 
method:POST 
url:http://httpbin.org/anything 
form:{'machine': 'machine_name'} 

Ansicht http://httpbin.org, es gibt viele andere url Endpunkte Sie testen können.

mit Python getestet: 3.4.2

Verwandte Themen