gpt4 book ai didi

python - 如何使用 python 请求设置内容类型的边界?

转载 作者:行者123 更新时间:2023-12-02 11:19:01 25 4
gpt4 key购买 nike

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

我该如何设置?或者我可以随机设置边界吗?

最佳答案

这是因为 header 值 ----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)

关于python - 如何使用 python 请求设置内容类型的边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51263978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com