1.導(dǎo)入包:
import? unittest
import requests
2.創(chuàng)建測試類:
class MyTestCase(unittest.TestCase):
? ? ? ? ? ? def setUp(self):
? ? ? ? ? ? ? ? ? ? print('開始')
? ? ? ? ? ? def test_001(self):
#1.url
? ? ? ? ? ? ? ? self.new_url ="http://youyi.52ruijiajia.com/admin/topTeacher/getTeacherList"
#2.header信息
?? ? ? ? self.header ={"Authorization":"zzzzzzzeyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ3ZW5sZSIsImFkbWluSWQiOjEsImV4cCI6MjE5MDE3NTc3MiwiaWF0IjoxNTg1Mzc1NzcyfQ.cHPLOxmMjgmj5nsSbTMN-mY0ymWVunwZyUfZO791UjCpua-AAGYZeXKdzXX8V_4MbjJq5hjTWSS1oHQiKxuFuQ"}
#3.json信息
????????????self.data = {"campus":"","level":"","name":"","type":"0","pageNum":1,"pageSize":10,"tel":""}
#4.創(chuàng)建post請求
????????????r = requests.post(self.new_url,headers =self.header,json=self.data)
#5.以json形式輸出
????????????res = r.json()
????????????self.assertEqual(res["code"],0)
????????????print(res["code"])
一玻粪、帶數(shù)據(jù)的post:
data = {'key1':'value1','key2':'value2'}
r = requests.post(url,data=data)
二金句、帶header的post
header = {"key1":"value"}
r = requests.post(url,headers = header)
三铛嘱、帶json的post:
data = {'key1':'value1','key2':'value2'}
header = {'key1':'value1'}
r = requests.post(url,headers = header,json= data)
四、帶參數(shù)的post:
header =?{'key1':'value1'}
params = {'key1':'param1','key2':'param2'}
data = {'key1':'value1','key2':'value2'}
r = requests.post(url,headers = header,params = params,json= data)
五、普通文件上傳:
# -*- 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)
六臂容、定制化文件上傳:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
#自定義文件名嗅剖,文件類型、請求頭
files = {
? ? ? ? 'file':('test.png',open('test.png','rb'),'image/png')
}
r = requests.post(url,files=files)
print (r.text)heman793
七矾利、多文件上傳:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
#多文件上傳
files = [
? ? ('file1',('test.txt',open('test.txt', 'rb'))),
? ? ('file2', ('test.png', open('test.png', 'rb')))
? ? ]
r = requests.post(url,files=files)
print (r.text)
八姑裂、流式上傳:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
#流式上傳
with open( 'test.txt' ) as f:
? ? r = requests.post(url,data = f)
print (r.text)