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