2017-03-24 5 views
0

versucht, eine cURL Anfrage an ein Gerät API mit Python AnfragenPython Anfragen an api Post - replizieren cURL

curl -qgsSkH "Content-Type: multipart/form-data" 
--no-progress-bar 
--header "X-Api-Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
-F "[email protected]/c/tmp/test.txt" 
-F "options={\"application\":\"2\",\"timeout\":\"500\",\"priority\":\"0\", 
\"profiles\":[\"win7-sp1\"],\"analysistype\":\"0\",\"force\":\"true\", 
\"prefetch\":\"0\"}" 
https://xxx.xxx.xxx.xxx:443/apis/v1.1.0/submissions 

Ich habe succefully an die API authentifiziert zu replizieren und die Recieved Token in der POST-Anforderung verwenden können. Meine aktuelle Python-Funktion ist unten gezeigt.

Die API gibt beim Versuch, POST zu senden, eine HTTP 400, Bad Request zurück. unten sind die Ergebnisse der Funktion

REQUEST HEADER: {'Content-Length': '424', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', 'User-Agent': 'python-requests/2.11.1', 'Connection': 'keep-alive', 'X-Api-Token': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'Content-Type': 'multipart/form-data'} 
REQUEST BODY: --63a06536e02c4479a846c5fe320cdf7f 
Content-Disposition: form-data; name="options" 

{"force": "false", "profiles": ["Win7 Sp1"], "priority": "0", "application": "-1", "prefetch": "value", "timeout": "240", "analysistype": "0"} 
--63a06536e02c4479a846c5fe320cdf7f 
Content-Disposition: form-data; name="filename"; filename="test.vbs" 

x = msgbox("this is an test file", 0, "test file") 
--63a06536e02c4479a846c5fe320cdf7f-- 

RESPONSE HEADER: {'Content-Length': '257', 'Expires': '0', 'Connection': 'close', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Date': 'Fri, 24 Mar 2017 17:11:47 GMT', 'X-Frame-Options': 'DENY', 'Content-Type': 'application/json;charset=ISO-8859-1'} 
RESPONSE CONTENT: {"apis":{"@version":"v1.1.0","description":"Error parsing options, please check you have provided a file for upload and included 'options' in json format as part of request","errorCode":"BESUB1001","httpStatus":400,"message":"Could not parse input"}} 
RESPONSE STATUS: 400 
RESPONSE REASON: Bad Request 

Process finished with exit code 0 

Was ich die Anfrage korrekt zu formatieren und senden Sie die Datei bin fehlt, über die cURL Anfrage zu replizieren?

+0

Ich empfehle Ihnen ein files = Dateien auf Ihren Post-Anforderung hinzufügen und entfernen es aus dem Datenbereich. http://stackoverflow.com/questions/22567306/python-requests-file-upload – Artagel

Antwort

0

öffnete die Datei nicht als Wörterbuch, bekam nur die Griff formatiert Optionen als JSON und enthalten in den „Dateien“

def api_file_submit(api_token, file_name): 
login_headers = {'X-Api-Token': api_token, 'Accept': '*/*'} 
files = open(file_name, 'rb') 
sub_options = {'application': '0', 'timeout': '240', 'priority': '0', 'profiles': ['win7-sp1'], 'analysistype': '2', 'force': 'false', 'prefetch': '1'} 
submissionData = ('', json.dumps(sub_options), 'application/json') 
files = {'options': submissionData, 'filename': files} 
r = requests.post(api_submit, headers=login_headers, files=files, verify=False)