RobotFramework_Python_API: 对于POST请求出现“异常”: “com.fasterxml.jackson.core.JsonParseException”
我正在尝试在Pycharm中使用Python的RobotFramework发送一个Post请求。每次执行时,我都会遇到一个“异常”:
com.fasterxml.jackson.core.JsonParseException。
正在执行的代码
TC2: Testing_createDict
${auth}= Create List Bxxxxxx xxxxxxxxxxxx
Create Session mysession ${base_url} auth=${auth}
${entry}= Create Dictionary Lot Status Code=C Item Number=95544403 Lot Expiration Date=09222021 Branch Plant=QIO Lot Description=BSIC-2022-001 Lot Number=185
${lot master}= Create List ${entry}
${body}= Create Dictionary Item Lot Check (Y/N)=N Lot Master=${lot master}
${header}= Create Dictionary Content-Type=application/json
${response}= POST On Session mysession /jderest/orchestrator/58_XXXXX_XXXX_ORCH data=${body} headers=${header}
Log To Console ${response.status_code}
输出结果:
[info (+0.25s)] POST Request : url=https://xxxxx//xxx
headers={'User-Agent': 'python-requests/2.28.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Content-Length': '182', 'Authorization': 'Basic xxxxxxxxxxxx'}
body=Item+Lot+Check+%28Y%2FN%29=N&Lot+Master=Lot+Status+Code&Lot+Master=Item+Number&Lot+Master=Lot+Expiration+Date&Lot+Master=Branch+Plant&Lot+Master=Lot+Description&Lot+Master=Lot+Number
[info] POST Response : url=https://xxxxxxxxx
status=500, reason=Internal Server Error
headers={'Date': 'Wed, 06 Mar 2024 15:09:46 GMT', 'Content-Type': 'application/json; charset=UTF-8', 'Content-Length': '425', 'Connection': 'keep-alive', 'Set-Cookie': 'XXXXX; Expires=Wed, 13 Mar 2024 15:09:46 GMT; Path=/, AWSALBCORS=XXXXXXXXX; Expires=Wed, 13 Mar 2024 15:09:46 GMT; Path=/; SameSite=None; Secure', 'X-ORACLE-DMS-ECID': 'xxx--xx-xxxx-xxxxx', 'X-ORACLE-DMS-RID': '0', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Origin': '*'}
body={
"message" : "Unrecognized token 'Item': was expecting 'null', 'true', 'false' or NaN\n at [Source: (String)\"Item+Lot+Check+%28Y%2FN%29=N&Lot+Master=Lot+Status+Code&Lot+Master=Item+Number&Lot+Master=Lot+Expiration+Date&Lot+Master=Branch+Plant&Lot+Master=Lot+Description&Lot+Master=Lot+Number\"; line: 1, column: 5]",
"exception" : "com.fasterxml.jackson.core.JsonParseException",
"timeStamp" : "2024-03-06:23.09.46"
}
1 个回答
0
根据请求库的文档,这里有个链接:https://requests.readthedocs.io/en/latest/user/quickstart/#more-complicated-post-requests
如果你需要设置那个头信息,并且不想自己去编码字典,你可以直接使用 json 参数(这个功能在 2.4.2 版本中加入),这样它会自动编码:
这里提到的是设置内容类型的头信息 并且 将字典编码成 json 格式。
在你的示例代码中,你传递的是纯字典 而没有 将其编码成 json,也没有手动设置内容类型。这很可能导致实际发送的数据使用了单引号,而远程解析器会因为 json 标准要求使用双引号而失败。
所以,你的代码
${response}= POST On Session mysession /jderest/orchestrator/58_XXXXX_XXXX_ORCH data=${body} headers=${header}
应该改成:
${response}= POST On Session mysession /jderest/orchestrator/58_XXXXX_XXXX_ORCH json=${body}
或者
${encoded_body} Evaluate json.dumps(${body}) modules=json
${response}= POST On Session mysession /jderest/orchestrator/58_XXXXX_XXXX_ORCH data=${body} headers=${header}