我有以下申请表(简体):POST /target_page HTTP/1.1Host: server_IP:8080Content-Type: multipart/form-data; boundary=AaaBbbCcc--AaaBbbCccContent-Disposition: form-data; name="json"Content-Type: application/json{ "param_1": "value_1", "param_2": "value_2"}--AaaBbbCccContent-Disposition: form-data; name="file"; filename="..."Content-Type: application/octet-stream<..file data..>--AaaBbbCcc--我试图用requests发送post请求:import requestsimport jsonfile = "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()但它返回status400并带有以下注释:Required request part \'json\' is not present.The request sent by the client was syntactically incorrect.请指出我的错误。我该怎么做才能成功? 最佳答案 您自己设置标题,包括一个边界。不要这样做;requests为您生成一个边界并在头中设置它,但是如果您已经设置了头,那么结果负载和头将不匹配。只需将标题全部删除:def send_request(): payload = {"param_1": "value_1", "param_2": "value_2"} files = { 'json': (None, json.dumps(payload), 'application/json'), 'file': (os.path.basename(file), open(file, 'rb'), 'application/octet-stream') } r = requests.post(url, files=files) print(r.content)注意,我还给了file部分文件名(路径的基名)。有关多部分post请求的更多信息,请参见advanced section of the documentation。