接口測試--post的請求實例

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)



部分參考:https://www.cnblogs.com/puresoul/p/7488700.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容