如何使用python请求设置Content-type的边界?

问题描述 投票:1回答:1
POST /write.php HTTP/1.1
HOST: upload.sitename.com
accept-encoding: gzip, deflate
content-length: 788
content-type: multipart/form-data; boundary=----WebKitFormBoundarycHb9L3gZr8gzENfz
referer: http://www.sitename.com
user-agent: sitename.app

------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="app_id";

123123123123
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="id";

idididid
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="mode";

write
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="subject";

subject
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="user_id";

456456456456
------WebKitFormBoundarycHb9L3gZr8gzENfz
Content-Disposition: form-data; name="memo_block[0]";

memoblock
------WebKitFormBoundarycHb9L3gZr8gzENfz--

我想使用python发送此请求。

import requests
from email.mime.multipart import MIMEMultipart

related = MIMEMultipart('form-data','----WebKitFormBoundary6o6EZLygJtLbQhjb')

headers = dict(related.items())
headers['User-Agent'] = 'sitename.app'
headers['referer'] = 'http://www.sitename.com'

files = {
    'app_id':(None, '123123'),
    'id':(None, 'idid'),
    'mode':(None, 'write'),
    'subject':(None, 'subject'),
    'user_id':(None, '456456'),
    'memo_block[0]':(None, 'memoblock')
}

req = requests.post('http://upload.sitename.com/_app_write_api.php', files=files, headers=headers)
print(req.status_code)

所以,我写了这样的代码。

我使用Wire Shark确定了请求,但这不是我想要的。

POST /_app_write_api.php HTTP/1.1
Host: upload.sitename.com
User-Agent: sitename.app
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Type: multipart/form-data; boundary="----WebKitFormBoundary6o6EZLygJtLbQhjb"
MIME-Version: 1.0
referer: http://www.sitename.com
Content-Length: 680

--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="app_id"

123123
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="id"

idid
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="mode"

write
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="subject"

subject
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="user_id"

456456
--e68a0877353c411b8972ec5f868b2e41
Content-Disposition: form-data; name="memo_block[0]"    

memoblock
--e68a0877353c411b8972ec5f868b2e41--

实际上发送这样的请求。服务器向我发送响应“错误”。

我认为它的发生是因为'--e68a0877353c411b8972ec5f868b2e41'。

我怎么设置这个?或者我可以随机设置边界吗?

python python-requests multipart boundary
1个回答
0
投票

这是因为标题值----WebKitFormBoundary6o6EZLygJtLbQhjb和body --e68a0877353c411b8972ec5f868b2e41不匹配。发送multipart/form-data你不需要设置标头Content-Type

import requests

headers = {
    'User-Agent' : 'sitename.app',
    'referer' : 'http://www.sitename.com'
}

files = {
    'app_id':(None, '123123'),
    'id':(None, 'idid'),
    'mode':(None, 'write'),
    'subject':(None, 'subject'),
    'user_id':(None, '456456'),
    'memo_block[0]':(None, 'memoblock')
}

req = requests.post('http://upload.sitename.com/_app_write_api.php', files=files, headers=headers)
print(req.status_code)
© www.soinside.com 2019 - 2024. All rights reserved.