I am now trying to submit multiple jobs into the web server http://helios.princeton.edu/CONCORD/ using the library requests.
My code looks currently like this:
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
The status returns 200, the headers return as follows, but the server does not submit the job, since I don't get any responses on my email.
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'}
Can anybody help me solve this and make it work?
You are trying to send URL query parameters while the form asks you to send multipart/form
encoded data:
<form id="query_form" action="" method="post" enctype="multipart/form-data">
Since the form is configured to use multipart/form-data
you should use either data
or files
to POST those parameters:
r = requests.post(post_url, files=info)
params
is for URL parameters , the part that comes after the ?
in a URL. You can use URL parameters in a POST request too , but form data typically is sent as part of the body instead. Using the files
argument, even with no actual file data, will trigger an encode to multipart/form-data
here.
Using the http://httpbin.org test server, compare the following responses:
>>> 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"
}
Note the difference in the url
parameter, as well as the fact that the first request shows parameters parsed into args
(from the URL), and the other in the files
parameter (from the POST body, using multipart/form-data
). Also note the difference in the Content-Type (missing in one) and the Content-Length headers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.