接口自动化中,入参为json格式报错:requests.exceptions.JSONDecodeError: [Errno Extra data] {"errors":{"":["Unexpected character encountered while parsing value: U. Path '', line 0, position 0."]}

import requests
import json
url = "http://10.1.12.101:33355/api/Login/CheckLogin"
head={
"Content-Type": "application/json;charset=UTF-8"
}
body={
"UserName": "admin",
"UserPassword": "202cb962ac59075b964b07152d234b70",
"funcCode": "DCARP",
"systemCode": "150"}

r = requests.post(url,data=body,headers=head)
result = r.json()
print(result)
运行报错:
requests.exceptions.JSONDecodeError: [Errno Extra data] {"errors":{"":["Unexpected character encountered while parsing value: U. Path '', line 0, position 0."]}
原因为入参格式为json格式需要使用json.dumps()将入参转换为jsOn格式
如下:
import requests
import json
url = "http://10.1.12.101:33355/api/Login/CheckLogin"
head={
"Content-Type": "application/json;charset=UTF-8"
}
body={
"UserName": "admin",
"UserPassword": "202cb962ac59075b964b07152d234b70",
"funcCode": "DCARP",
"systemCode": "150"}
r = requests.post(url, data=json.dumps(body),headers=head) #使用json.dumps()将一个Python数据结构转换为JSON
print(r.status_code)
result = r.json()
print(result)

posted @   土孩子  阅读(4382)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示