一、GET请求
1、发送GET请求
输入:url
方法:requests.get(url)
import requests
r=requests.get('https://api.github.com/events')
输出:Response对象
2、发送包含Query String的GET 请求
输入:url, dict
方法:requests.get(url, params=dict)
输出:Response对象
二、POST请求
python requests 2 : GET 、 POST请求_weixin_xyyqwl的博客-优快云博客
1、使用dict发送包含表单的POST请求
输入:url, dict
方法:requests.post(url, data=dict)
输出:Response对象
2、使用tuple发送包含表单的POST请求
输入:url, tuple
方法:requests.post(url, data=tuple)
输出:Response对象
3、dict转换成string ,发送包含json的POST请求
python requests 2 : GET 、 POST请求_weixin_xyyqwl的博客-优快云博客
输入:url, string
方法:
json.dumps(dict)
requests.post(url, data=string)
输出:Response对象
4、直接使用dict,发送包含json的POST请求
输入:url, dict
方法:
requests.post(url, json=dict)
输出:Response对象
5、发送更改headers的POST请求
输入:url, dict,string
方法:requests.get(url, headers=dict,data=string)
输出:Response对象
例1:指定Content-Type为application/json
>>> import json
>>> payload={'key1':'value1'}
>>> jstr=json.dumps(payload)
>>> h1={'Content-Type': 'application/json'}
>>> type(jstr)
<class 'str'>
>>> type(h1)
<class 'dict'>
>>> r=requests.post("http://httpbin.org/post",headers=h1,data=jstr)
>>> print(r.text)
https://blog.youkuaiyun.com/weixin_51380973/article/details/123739184?spm=1001.2014.3001.5501
例2:指定Content-Type为application/json
>>> import json
>>> payload={'key1':'value1'}
>>> jstr=json.dumps(payload)
>>> h1={'Content-Type': 'x-www-form-urlencoded;charset=UTF-8'}
>>> r=requests.post("http://httpbin.org/post",headers=h1,data=jstr)
>>> print(r.text)