request用法详细讲解

request用法详细讲解

post的简单用法
# -- coding:utf-8 --
import requests
import json

host = http://httpbin.org/
endpoint = “post”
url = ‘’.join([host,endpoint])
data = {‘key1’:‘value1’,‘key2’:‘value2’}

r = requests.post(url,data=data)
#response = r.json()
print (r.text)

带json的输入参数
# -- coding:utf-8 --
import requests
import json

host = “http://httpbin.org/
endpoint = “post”

url = ‘’.join([host,endpoint])
data = {
“sites”: [
{ “name”:”test” , “url”:”www.test.com“ },
{ “name”:”google” , “url”:”www.google.com“ },
{ “name”:”weibo” , “url”:”www.weibo.com“ }
]
}

r = requests.post(url,json=data)
# r = requests.post(url,data=json.dumps(data))
response = r.json()

普通文件上传
# -- coding:utf-8 --
import requests
import json

host = “http://httpbin.org/
endpoint = “post”

url = ‘’.join([host,endpoint])
#普通上传
files = {
‘file’:open(‘test.txt’,’rb’)
}

r = requests.post(url,files=files)
print (r.text)

requests 跳过ssl验证

  1. res = requests.post(url=url, headers=headers, data=data, verify=True).json()
  2. token = res['data']['token']
  3. ptaxxzxh5UserId = res['data']['ptaxxzxh5UserId']

标签