【发布时间】: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