将请求的各个参数用Prepared Request数据结构表示, 简单示例:
代码:
from requests import Request, Sessionurl = 'http://httpbin.org/post'
data = {'name': 'gyt'
}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36'
}
s = Session() #创建Session对象
req = Request('POST', url, data=data, headers=headers) #创建Request对象
prepped = s.prepare_request(req) #将Request对象转换为prepare_request
r = s.send(prepped) #发送
print(r.text)
结果:
{"args":{},"data":"","files":{},"form":{"name":"gyt"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"8","Content-Type":"application/x-www-form-urlencoded","Host":"httpbin.org","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"},"json":null,"origin":"118.113.89.18","url":"http://httpbin.org/post"}