【问题标题】:Internal Server Error with an API POST call using Python script使用 Python 脚本进行 API POST 调用的内部服务器错误
【发布时间】:2021-09-08 16:20:01
【问题描述】:

我正在尝试编写此代码以向组织发送 API 调用。 但是,我正在构建的 jsonRecord 在使用 print 时显示正确的值,但是在通过 API 请求调用使用它时却给了我一个错误。 下面是代码:

cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

cursor.execute("set nocount on; set rowcount 1; select Right([Code], 5) as courseNumber,  PresentationNumber as presentationNumber, Left ([Date], 4) as fiscalYear, CordinatorEmail as [coordinatorInfo.email], CordinatorFName as [coordinatorInfo.firstName], CordinatorLName as  [coordinatorInfo.lastName],  CordinatorMName as  [coordinatorInfo.middleName], CordinatorTelNumber as [coordinatorInfo.telephone] from bctc.trainingtest.tblClassesALL where (PostCorStatus is NULL and CertType = 'POST') FOR JSON PATH, WITHOUT_ARRAY_WRAPPER")
 
row  = cursor.fetchone() 
while row:
 jRecord = row[0]
 print (jRecord)

 row = cursor.fetchone()
 
 url = "https://dev.post.ca.gov/CourseRostersWebService/Services/CourseRosters.svc/json/UpdateCoordinatorInfo"

 headers = {"Authorization":"Basic dxxxxxxxxxxxxxxx==","content-type":"application/json;charset=UTF-8"}
 
 
 y = requests.post(url, json = jRecord , headers = headers , verify=True)

 

 print(y.text)
 print(y.reason)
 print(y.json())
 print(y.status_code)
 print("response headers >>>>", y.headers)

这是错误:

{"courseNumber":"30060","presentationNumber":"001","fiscalYear":"2021","coordinatorInfo":{"email":"mawad@test.org","firstName":"Maged","lastName":"Awad","middleName":"K","telephone":"(718)258-9631"}}
{"errorCode":100,"errorDescription":"Unspecified Error Occurred"}
Internal Server Error
{'errorCode': 100, 'errorDescription': 'Unspecified Error Occurred'}
500
response headers >>>> {'Cache-Control': 'private', 'Content-Type': 'application/xml; charset=utf-8', 'Server': 'Microsoft-IIS/10.0', 'X-AspNet-Version': '4.0.30319', 'X-Powered-By': 'ASP.NET', 'X-UA-Compatible': 'IE=edge', 'Date': 'Wed, 08 Sep 2021 16:15:34 GMT', 'Content-Length': '65'}

我还想提一下,当我将 jRecord 硬编码为 json 记录时,它起作用了。

不确定构建该对象是否错误,因为它正在正确显示。

有人可以帮忙吗?

【问题讨论】:

  • 为什么将 ROWCOUNT 设置为 1? python代码调试时返回什么错误?

标签: python sql-server


【解决方案1】:

我需要从 SQL 表中提取一条记录来处理和发送 API 调用。 我通过简单地添加这行代码解决了这个问题:

jRecord2 = json.loads(jRecord)

这会将 JSON SQL 输出从字符串转换为 JSON。

否则请求调用无法识别对象 SJON 格式,并且在向端点发送调用之前失败。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-13
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    相关资源
    最近更新 更多