无法解码 JSON 正文:查找值开头的字符"q"无效



我已经成功地解析了我所拥有的json文件,通过在json文件中需要全局变量的全局变量中输入值。但是,当我尝试用该id点击请求时,我会得到响应:{'errors':[{'message':"json主体无法解码:无效字符'q'正在查找值的开头"}],'data':None}

请帮助我添加和修复什么?

这是我的测试用例:

TC Edit Mission Detail For Input Target Task
[Documentation]             Example tc
[Tags]                      api test
Set Test ID                 9452
${payload}=         Get File    api-test/Main/collections/engagement/testing/intools/edit_mission.json
${payload}=         Evaluate    json.loads(${payload})    modules=json
Set To Dictionary   ${payload['variables']}   id    544
Log To Console      ${payload}
# Req body
${response}=                GraphQl Request     method=POST
...                         referrer_url=graphql/query
...                         payload_path=${payload}
...                         token=${token}
Set Global Variable         ${response}
Log To Console              ${response}
## Assertion
${expected_json}            Get File                    api-test/Main/assertions/expected-json/engagement/edit_mission.json
${expected_json}=           Convert To Json             ${expected_json}

我的关键字"GraphQl请求">

@keyword("GraphQl Request")
def GraphQlRequest(method, referrer_url, payload_path=None, token=None, image=None):
url = "https://testingstaging.co/" + str(referrer_url) + ""
print(url)
if payload_path is not None:
payload = payload_path
else:
payload = {'operations': '{ "query": "mutation ($file: Upload!) { uploadImage(file: $file) { message } }", "variables": { "file": null } }',
'map': '{ "0": ["variables.file"] }'}
if token is not None:
token = token
if image is None:
headers = {
'Authorization': 'Bearer ' + str(token) + '',
'Content-Type': 'application/json',
}
else:
headers = {
'Authorization': 'Bearer ' + str(token) + '',
'Origin': 'https://testing123.co.id'
}
print(headers)

if image is None:
response = requests.request("" + str(method) + "", url, headers=headers, data=payload)
print(json.dumps(response.json(), indent=2))
return response.json()
elif Path(image).suffix == '.csv':
payload={'operations': '{"query":"mutation ($testingFile: Upload!) {\n  createCampaign(input: {\n    name:\"Promo\",\n    startDate:\"2020-01-02T00:00:00+07:00\",\n    endDate:\"2020-06-06T00:00:00+07:00\",\n    type:\"first_order\",\n    variables:\"{\\\"orderAmount\\\": 2000, \\\"orderOperation\\\": \\\"=\\\"}\",\n    testing: $testing\n  }) {\n    message\n  }}","variables":{"testing":null}}',
'map': '{ "0": ["variables.testing"] }'}
files=[
('0',(''+str(image)+'',open('api-test/Main/assets/'+str(image)+'','rb'),'text/csv'))
]
response = requests.request("" + str(method) + "", url, headers=headers, data=payload, files=files)
print(json.dumps(response.json(), indent=2))
return response.json()
else:
files=[
('0',(''+str(image)+'',open('api-test/Main/assets/'+str(image)+'','rb'),'image/jpg'))
]
response = requests.request("" + str(method) + "", url, headers=headers, data=payload, files=files)
print(json.dumps(response.json(), indent=2))
return response.json()

以下是回应:

Edit Mission Detail For Input Target Task :: This test for input t... 
....{'query': 'mutation CreateMission($id: Int!, $details: 
String!) {rn  UpdateMissionDetail(input: { id: $id, details: 
$details })rn}rn', 'variables': {'details': '{"total_amount": 
518800}', 'id': '544'}}...{'errors': [{'message': "json body could not 
be decoded: invalid character 'q' looking for beginning of 
value"}],'data': None}

您正受到JSON内容中使用"的影响。您可以尝试用\"转义它们,或者用单引号'替换它们。

相关内容

最新更新