2
votes

I am trying to upload a csv file into Azure Devops workitem. While uploading the attachment step calling below rest api, I am facing an issue: POST https://dev.azure.com/{organization}/{project}/_apis/wit/attachments?api-version=5.1

My code is as below:

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)

Same code is working when I am using python 3.8 in my local visual studio code but not working when I am using Azure Automation Runbook (python 2.7).

When I try to print text of the response body, I am getting below error:

print(ado_req_attach_File.text)UnicodeEncodeError: 'ascii' codec can't encode character u'\u221e' in position 6303: ordinal not in range(128)

Expected Output:

{"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"}

1
Hi vivek Mishra, Did you get a chance to implement the solution that I suggested? Were you able to resolve? If my reply helped. Appreciate for marking it as an answer which will also help others in the community.PatrickLu-MSFT

1 Answers

0
votes

This seems not related to Azure DevOps side, since your API works properly. And you have got the JSON response succeed.

For version 2.7 ,encoding is not set by default.

Please use below code as a first line of program and check if it works:

# -*- coding: utf-8 -*-

# Your code goes below this line

For python 3.x ,there is default encoding.hence there will be no issue of encoding.

Also take a look at similar issue here for more info: https://stackoverflow.com/a/39293287/5391065