我试图上传一个csv文件到Azure工作项。在上传rest下面调用的附件步骤时,我面临一个问题: POST https://dev.azure.com/{organization}/{project}/_apis/wit/attachments?api-version=5.1
我的代码如下:
with open('details.csv', 'r') as f:
details = f.read()
print(details) #Printing the CSV file as expected
ado_req_headers_ATT = {'Content-Type':'application/octet-stream'}
ADO_SEC_ATTA_URL = 'https://dev.azure.com/orgname/projectname/_apis/wit/attachments?fileName=details.csv&api-version=5.1-preview.3'
ado_req_attach_File = requests.post(url=ADO_SEC_ATTA_URL,headers=ado_req_headers_ATT,data=details, auth=('',ADO_AUTH_PAT))
print(ado_req_attach_File.text)当我在本地visual代码中使用python3.8时,同样的代码也在工作,但当我使用(python2.7)时,代码就不能工作了。
当我试图打印响应正文的文本时,我得到的错误如下:
print(ado_req_attach_File.text)UnicodeEncodeError:'ascii‘编解码器不能在位置6303编码字符u’\u221e:序数不在范围内(128)
预期输出:
{"id":"facedff6-48c6-5479-894b-f7807f29b96e","url":"https://dev.azure.com/orgname/d93740f8-fe37-5433-bc8e-79c0a320d81b/_apis/wit/attachments/facedff6-48c6-5479-894b-f7807f29b96e?fileName=details.csv"}“
发布于 2020-08-10 09:15:47
这似乎与Azure DevOps端无关,因为您的API工作正常。您的JSON响应成功了。
对于2.7版本,默认情况下不设置编码。
请使用下面的代码作为第一行程序,并检查它是否有效:
# -*- coding: utf-8 -*-
# Your code goes below this line对于python3.x,默认的encoding.hence不会出现编码问题。
还可以在这里查看类似的问题,以获得更多信息:https://stackoverflow.com/a/39293287/5391065
https://stackoverflow.com/questions/63330592
复制相似问题