Home > Back-end >  Change project Avatar with Azure DevOPS API 6.0-preview.1 and python
Change project Avatar with Azure DevOPS API 6.0-preview.1 and python

Time:10-19

I'm trying to change the Project Avatar/picture for a project in Azure DevOps with the api. For that I'm using a python script. The api docu states that the picture has to be send as a byte array. But the type is descripted as string array. So for me it sounds like the byte array has to be converted to a string and then send in a json.

In python this looks like this:

url = f'https://dev.azure.com/{organization}/_apis/projects/{project_id}/avatar?api-version=6.0-preview.1 '
with open(project_picture_path, "rb") as image:
    image_array = []
    while byte := image.read(1):
        image_array.append(byte)
    image_array = str(image_array)
json1 = json.dumps({"image": image_array})
headers = {'Content-type': 'application/json'}
r = requests.put(url, data=json1, auth=auth, headers=headers)
print(r.status_code)

When I send this put request to the server it just responds with a 204 http code, which is odd in it self because the docu states it should respond with a 200.

When I go to the project page in DevOps the picture has indeed changed, but not to my specified picture. Instead from the default to what seems to be a placeholder image. You can see a comparison here

I don't have any idea what went wrong here. Did anyone else tried doing that?

CodePudding user response:

After lots of testing I found a solution.

The api wants to get the bytes as int values so we have to convert them to int like this:

with open(project_picture_path, "rb") as image:
    image_array = []
    while byte := image.read(1):
        image_array.append(int.from_bytes(byte, 'big'))
json1 = json.dumps({"image": image_array})
headers = {'Content-type': 'application/json'}
r = requests.put(url, data=json1, auth=auth, headers=headers)
print(r.status_code)

This way the api still gives a 204 response code but the picture changes correctly. This should definitely be changed in the docs.

  • Related