[英]Why my POST request doesn't seem to work? (Python requests module)
我现在正尝试使用库请求将多个作业提交到Web服务器http://helios.princeton.edu/CONCORD/中 。
我的代码当前看起来像这样:
import requests
post_url = "http://helios.princeton.edu/CONCORD"
response_email = "email_example@gmail.com"
sequence_example = "MLGDTMSGGIRGHTHLAIMAVFKMSPGYVLGVFLRKLTSRETALMVIGMAMTTTLSIPHDLMELIDGISLGLILLKIVTQFDNTQVG"
job_description = "example"
info = { "sequence_text": sequence_example, "email_address": response_email, "description": job_description }
r = requests.post(post_url, params=info)
print r.status_code
print r.headers
状态返回200,标题返回,如下所示,但是服务器未提交作业,因为我的电子邮件没有任何响应。
200
{'content-length': '5637', 'x-powered-by': 'PHP/5.3.2', 'server': 'Apache/2.2.3 (Red Hat)', 'connection': 'close', 'date': 'Mon, 09 Mar 2015 14:14:33 GMT', 'content-type': 'text/html; charset=UTF-8'}
有人可以帮助我解决这个问题并使它起作用吗?
您正在尝试发送URL查询参数,而表单要求您发送multipart/form
编码的数据:
<form id="query_form" action="" method="post" enctype="multipart/form-data">
由于将表单配置为使用multipart/form-data
您应该使用data
或files
来发布这些参数:
r = requests.post(post_url, files=info)
params
是用于URL参数的 ,在?
之后的部分是?
在网址中。 您可以在POST请求使用URL参数太多 ,但表单数据通常被发送,而不是作为身体的一部分。 即使没有实际文件数据,使用files
参数也会在此处触发对multipart/form-data
的编码。
使用http://httpbin.org测试服务器,比较以下响应:
>>> import requests
>>> info = {"sequence_text": 'sequence', "email_address": 'email', "description": 'desc'}
>>> print requests.post('http://httpbin.org/post', params=info).text
{
"args": {
"description": "desc",
"email_address": "email",
"sequence_text": "sequence"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.5.1 CPython/2.7.9 Darwin/14.3.0"
},
"json": null,
"origin": "81.134.152.4",
"url": "http://httpbin.org/post?description=desc&email_address=email&sequence_text=sequence"
}
>>> print requests.post('http://httpbin.org/post', files=info).text
{
"args": {},
"data": "",
"files": {
"description": "desc",
"email_address": "email",
"sequence_text": "sequence"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "411",
"Content-Type": "multipart/form-data; boundary=be9a69498ab445b1a79282584877b3bf",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.5.1 CPython/2.7.9 Darwin/14.3.0"
},
"json": null,
"origin": "81.134.152.4",
"url": "http://httpbin.org/post"
}
注意url
参数的区别,以及第一个请求显示了解析为args
(来自URL),另一个显示了files
参数的事实(来自POST正文,使用multipart/form-data
)。 另请注意,Content-Type(缺少一个)和Content-Length标头的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.