2017-09-05 6 views
0

複数のフォームパラメータを指定する必要があるプロジェクトを処理しています。どちらがファイルそのものかを指定します。要求に応じて複数のフォームパラメータを送信するにはどうすればよいですか?

私が試した何

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) 

問題

私がいる場合と仮定し

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”) 

(これらのすべては、フォームパラメータです)このような追加情報を渡す必要があります私もこのように作成することができます形式でマシン名を送信したいですか?

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

任意の提案が役立ちます。

+0

はい、インポートが修正さDAS-Gタイプ@ –

+0

を要求 –

答えて

0

質問:マシン名をフォームにも送信したい場合は、このように作成できますか?あなたがrequestsパラメータをテストしたい場合は、以下を実行することができます


要求クイックスタートMore complicated POST requests

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])) 

出力

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'} 

ビューhttp://httpbin.org、あなたがテストすることができ他の多くのURLエンドポイントがあります。パイソンでテスト

:3.4.2

関連する問題