python,Jmeter實現application/json的post請求


說明:本文中使用的token來自同分類(接口測試)的token獲取方法【https://i-beta.cnblogs.com/posts/edit;postId=11828532

  • python方法

json.dumps()用於將dict類型的數據轉成str,因為如果直接將dict類型的數據寫入json文件中會發生報錯,因此在將數據寫入時需要用到該函數

import requests
import json
import random
from Test.token import Token
class createMessage(Token):
    create_url = 'http://xxxxx.com/xxx/xxx'
    header = {
        'Authorization': 'Bearer ' + Token.token,
        'Content-Type': 'application/json; charset=UTF-8'
    }   # Authonorization需結合實際要求
    datas =[{
        'order_id':'1570000000000',
        'type':'EMS',    
     'order_number':random.randint(10000000,9999999999999999), 'remarks':'2019-11-7', }] # 一般傳為字典格式,此處傳在字典外加[],因為要求請求參數傳為json的二維數組格式,不加為一維數組格式 datas_json = json.dumps(datas) # 請求數據進行json編碼,轉化為字符串格式 res = requests.post(url=create_url,data=datas_json,headers =header) print(res.headers) print(res.text) if '__name__' == '__main__': createmessage= createMessage()

測試結果為:

res.text的結果為:
{"status":"200","message":"OK","data":{"code":"0000","result":"Request is Success","remarks":"請求成功","data":{"access_token":"74bf187e4400524df2f2033fd9e8279c9a11a3020b659d7c49f04a179f59158e","token_type":"Bearer","expires_in":7200}}}
res.text_json的結果為:
{'message': 'OK', 'data': {'remarks': '請求成功', 'result': 'Request is Success', 'data': {'access_token': '74bf187e4400524df2f2033fd9e8279c9a11a3020b659d7c49f04a179f59158e', 'expires_in': 7200, 'token_type': 'Bearer'}, 'code': '0000'}, 'status': '200'}
token為:
74bf187e4400524df2f2033fd9e8279c9a11a3020b659d7c49f04a179f59158e
請求頭:
{'Authorization': 'Bearer 74bf187e4400524df2f2033fd9e8279c9a11a3020b659d7c49f04a179f59158e', 'Content-Type': 'application/json; charset=UTF-8'}
請求參數為
[{"remarks": "2019-11-7", "delivery_type": "EMS", "delivery_number": 8084113305043046, "order_id": "1573435888483536"}]
響應頭為:
{'Connection': 'keep-alive', 'Keep-Alive': 'timeout=60', 'Set-Cookie': 'PHPSESSID=5eheip3ht6gmnabamt6u103ri1; path=/, PHPSESSID=5eheip3ht6gmnabamt6u103ri1; expires=Mon, 11-Nov-2019 06:25:45 GMT; Max-Age=1440; path=/', 'Date': 'Mon, 11 Nov 2019 06:01:46 GMT', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Transfer-Encoding': 'chunked', 'Content-Encoding': 'gzip', 'X-Powered-By': 'PHP/5.6.40', 'Content-Type': 'text/html; charset=utf-8', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Pragma': 'no-cache', 'Server': 'Tengine'}
響應體為:
{"status":"200","message":"OK","data":{"code":"0000","result":"Request is Success","remarks":"請求成功","data":[{"order_id":"1573435888483536","delivery_id":1573452106,"error":false,"desc":""}]}}

  • Jmeter方法

  1. 添加在與token同一個線程下,添加HTTP請求

  2.在【HTTP請求】下添加HTTP信息頭管理器(添加---配置元件---HTTP信息頭管理器)

 

 

 3.添加監聽器,執行測試,查看響應結果

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM