接口自動化中,入參為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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM