900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > python3+requests:get post请求(python get post)

python3+requests:get post请求(python get post)

时间:2024-07-05 23:42:09

相关推荐

python3+requests:get post请求(python get post)

1.get请求

(1)没有请求参数类型

response = requests.get(url='')print(response.text)

(2)有请求参数的类型(键值对形式表示参数)

response = requests.get(url='',params={'key1':'value1','key2':'value2'})print(response.text)

(3)有请求头(键值对形式表示请求头)

response = requests.get(url='',headers={'key1':'value1'})print(response.text)

2.post请求

(1)请求正文是application/x-www-form-urlencoded

res = requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'application/x-www-form-urlencoded'})print (res.json)print (res.text)

(2)请求正文是multipart/form-data

res = requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'multipart/form-data'})print (res.json)print (res.text)

(3)请求正文是raw

传入xml格式文本

res = requests.post(url='',data='<?xml ?>',headers={'Content-Type':'text/xml'})print (res.json)print (res.text)

传入json格式文本

res = requests.post(url='',data=json.dumps({'key1':'value1','key2':'value2'}),headers={'Content-Type':'application/json'})print (res.json)print (res.text)

或者

res = requests.post(url='',json={{'key1':'value1','key2':'value2'}},headers={'Content-Type':'application/json'})print (res.json)print (res.text)

(4)请求正文是binary

res = requests.post(url='',files={'file':open('test.xls','rb')},headers={'Content-Type':'binary'})print (res.json)print (res.text)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。