一尘不染

如何将JSON作为多部分POST请求的一部分发送

json

我有以下POST请求表格(简化):

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

我尝试发送POST请求requests

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

但它返回状态400并带有以下注释:

Required request part \'json\' is not present.
The request sent by the client was syntactically incorrect.

请指出我的错误。我应该进行哪些更改才能使其正常工作?


阅读 329

收藏
2020-07-27

共1个答案

一尘不染

您自己设置标题,包括边界。不要这样 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零件一个文件名(file路径的基本名称)。

有关多部分POST请求的更多信息,请参阅文档高级部分

2020-07-27