So wird JSON als Teil einer mehrteiligen POST-Anfrage gesendet
Ich habe folgendes POST-Anfrageformular (vereinfacht):
POST /target_page HTTP/1.1
Host: server_IP:8080
Content-Type: multipart/form-data; boundary=AaaBbbCcc
--AaaBbbCcc
Content-Disposition: form-data; name="json"
Content-Type: application/json
{ "param_1": "value_1", "param_2": "value_2"}
--AaaBbbCcc
Content-Disposition: form-data; name="file"; filename="..."
Content-Type: application/octet-stream
<..file data..>
--AaaBbbCcc--
Ich versuche eine POST-Anfrage mit @ zu sendrequests
:
import requests
import json
file = "C:\\Path\\To\\File\\file.zip"
url = 'http://server_IP:8080/target_page'
def send_request():
headers = {'Content-type': 'multipart/form-data; boundary=AaaBbbCcc'}
payload = { "param_1": "value_1", "param_2": "value_2"}
r = requests.post(url, files={'json': (None, json.dumps(payload), 'application/json'), 'file': (open(file, 'rb'), 'application/octet-stream')}, headers=headers)
print(r.content)
if __name__ == '__main__':
send_request()
aber es wird der Status @ zurückgegeb400
mit folgendem Kommentar:
Required request part \'json\' is not present.
The request sent by the client was syntactically incorrect.
Bitte weisen Sie auf meinen Fehler hin. Was soll ich ändern, damit es funktioniert?