问题是请求数据为对象,接口返回错误,对json知识还不够熟悉,导致了这个小错误,后面要仔细复习哈json知识.
打印的数据为单引号就是对象(字典),为双引号就是json
转为json格式:json.dumps()*
错误代码如下:
#coding:utf-8
import json
from base.runmethod import RunMethod
from utile.operation_json import OperationJson
from utile.operation_excel import OperationExcel
class Run_monitor_add:
def __init__(self):
self.runmethod = RunMethod()
# self.opexcel = OperationExcel()
self.url = "xxxx"
def go_run(self):
opjson = OperationJson("../dataconfig/testJson.json").read_data()
print(opjson)
headers={'Content-Type':'application/json'}
res=self.runmethod.run_main('post',self.url,opjson,header=headers)
print(res)
if __name__ == '__main__':
runmain=Run_monitor_add()
runmain.go_run()
报错信息:
修改代码后
#coding:utf-8
import json
from base.runmethod import RunMethod
from utile.operation_json import OperationJson
from utile.operation_excel import OperationExcel
class Run_monitor_add:
def __init__(self):
self.runmethod = RunMethod()
self.url = "xxxx"
def go_run(self):
opjson = OperationJson("../dataconfig/testJson.json").read_data()
#对象转json
a=json.dumps(opjson)
print(a)
headers={'Content-Type':'application/json'}
res=self.runmethod.run_main('post',self.url,a,header=headers)
print(res)
if __name__ == '__main__':
runmain=Run_monitor_add()
runmain.go_run()
返回成功信息