Python使⽤requests发送POST请求总结
⼀个http请求包括三个部分,为别为请求⾏,请求报头,消息主体,类似以下这样:
请求⾏
请求报头
消息主体
HTTP协议规定post提交的数据必须放在消息主体中,但是协议并没有规定必须使⽤什么编码⽅式。服务端通过是根据请求头中的Content-Type字段来获知请求中的消息主体是⽤何种⽅式进⾏编码,再对消息主体进⾏解析。具体的编码⽅式包括:
application/x-www-form-urlencoded
最常见post提交数据的⽅式,以form表单形式提交数据。
application/json
以json串提交数据。
multipart/form-data
⼀般使⽤来上传⽂件。
macos是什么意思2.7.1 以form形式发送post请求
python请求并解析json数据Reqeusts⽀持以form表单形式发送post请求,只需要将请求的参数构造成⼀个字典,然后传给requests.post()的data参数即可。
# -*- coding: utf-8 -*-
import requests
import json
def send_requests1():
url = '/post'
d = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=d)
return r
def send_requests2():
url = '/post'
s = json.dumps({'key1': 'value1', 'key2': 'value2'})
r = requests.post(url, data=s)
return r
def send_requests3():
url = '/post'
files = {'file': open('', 'rb')}
r = requests.post(url, files=files)
return r
if __name__ == "__main__":
response1 = send_requests1()
print '------ = ',
# response2 = send_requests2()
# print '------ = ',
#
# response3 = send_requests3()
# print '------ = ',
输出:
------ = {
"args": {},
"data": "",
vim快捷键命令大全"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded", "Host": "",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "220.194.45.154, 220.194.45.154",
"url": "/post"
}
可以看到,请求头中的Content-Type字段已设置为application/x-www-form-urlencoded,且d = {'key1': 'value1', 'key2': 'value2'}以form表单的形式提交到服务端,服务端返回的form字段即是提交的数据。
2.7.2 以json形式发送post请求
cait是什么数据库可以将⼀json串传给requests.post()的data参数
# -*- coding: utf-8 -*-
import requests
import json
def send_requests1():
url = '/post'
d = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=d)
return r
def send_requests2():
url = '/post'
s = json.dumps({'key1': 'value1', 'key2': 'value2'})
r = requests.post(url, data=s)
return r
def send_requests3():
url = '/post'
files = {'file': open('', 'rb')}
r = requests.post(url, files=files)
return r
if __name__ == "__main__":
# response1 = send_requests1()
# print '------ = ',
response2 = send_requests2()
print '------ = ',
#
# response3 = send_requests3()
# print '------ = ',
输出:
------ = {
"args": {},
"data": "{\"key2\": \"value2\", \"key1\": \"value1\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "36",
"Host": "",
"User-Agent": "python-requests/2.18.1"
},
"json": {
"key1": "value1",
"key2": "value2"
},
"origin": "220.194.45.154, 220.194.45.154",
"url": "/post"
}
怎么新建一个数据库可以看到,请求头的Content-Type设置为application/json,并将s这个json串提交到服务端中。2.7.3 以multipart形式发送post请求
Requests也⽀持以multipart形式发送post请求,只需将⼀⽂件传给requests.post()的files参数即可。
# -*- coding: utf-8 -*-
import requests
import json
def send_requests1():
url = '/post'
d = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=d)
return r
def send_requests2():
url = '/post'
s = json.dumps({'key1': 'value1', 'key2': 'value2'})
r = requests.post(url, data=s)
return r
def send_requests3():
url = '/post'
files = {'file': open('', 'rb')}
r = requests.post(url, files=files)
return r
if __name__ == "__main__":
# response1 = send_requests1()
# print '------ = ',
# response2 = send_requests2()
# print '------ = ',
response3 = send_requests3()
print '------ = ',
输出:
------ = {
"args": {},
"data": "",
"files": {
"file": "hello word!\n"
},
哥斯达黎加vs西班牙比分预测最新"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "158",
"Content-Type": "multipart/form-data; boundary=0562a8553c254ba298a69aa9e7fb477e",
"Host": "",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "220.194.45.154, 220.194.45.154",
"url": "/post"
}
⽂本⽂件的内容只有⼀⾏:Hello world!,从请求的响应结果可以看到数据已上传到服务端中。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论