jsonimagepostazure-devops

Error in adding an image to Azure Devops with API POST


I'm trying to add an image in a field in my Azure Devops, but I keep getting this error:

the image stays like that

I'm using this code to store it into the text box:

{
    "op": "add",
    "path": "/fields/System.Description",
    "from": null,
    "value": "<div><img src=%22https%3A//dev.azure.com/myorg/mytoken/_apis/wit/attachments/8c339e7d-a25e-4980-93bc-87fa2eaba43e?fileName=dssdfsdfsdf.png%22><br></div>"
}

When I click in that "blank" image, I get a page with this information:

{
    "$id": "1",
    "innerException": null,
    "message": "A potentially dangerous Request.Path value was detected from the client (:).",
    "typeName": "System.Web.HttpException, System.Web",
    "typeKey": "HttpException",
    "errorCode": 0,
    "eventId": 0
}

The API request that I'm doing is:

POST https://dev.azure.com/{organization}/{project}/_apis/wit/attachments?api-version=7.1-preview.3

It does give me a success response, and returns the link that I entered in the <img src= element.

But then the image isn't being shown.

I tried changing the ":" in the img source to an %3A but it doesn't work either.


Solution

  • The rest api is used to add attachment only, cannot image add to work item description field.

    POST https://dev.azure.com/{organization}/{project}/_apis/wit/attachments?api-version=7.1-preview.3
    

    With above rest api, you can get the attached image url:

    $organization = "ORG"
    $project = "PROJECT"
    $fileName = "testimage.png"
    $personalAccessToken = "YourPAT"
    $filePath = "C:\Users\wade\Downloads\testimage.png"   # image file path
    
    $uploadUrl = "https://dev.azure.com/$organization/$project/_apis/wit/attachments?fileName=$fileName&api-version=7.1-preview.3"
    
    $headers = @{
        Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
    }
    
    $response = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $headers -InFile $filePath -ContentType 'application/octet-stream'
    
    $imageUrl = $response.url
    
    Write-Host The imageurl is: $imageUrl
    

    enter image description here

    With the image url, you can use rest api Work Items - Update to add image to the work item description field. Change the work item id to yours.

    $organization = "ORG"
    $project = "Project"
    $personalAccessToken = "YourPAT"
    
    $workItemId = "498"        # change to use your work item id
    $updateUrl = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$workItemId"+ "?api-version=7.1-preview.3"
    
    $imageUrl= "your image url get from above rest api"
    
    $description = "<div>add testimage in description<img src='$imageUrl' 'alt=testimage.png'></div>"
    
    
    $body = @(
        @{
            op = "add"
            path = "/fields/System.Description"
            value = $description
        }
    )
    
    
    $headers = @{
        Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
    }
    
    Invoke-RestMethod -Uri $updateUrl -Method Patch -Headers $headers -Body (ConvertTo-Json -InputObject $body) -ContentType 'application/json-patch+json'
    

    Image added to the description:

    enter image description here

    The whole script is below:

    $organization = "ORG"
    $project = "Project"
    $fileName = "testimage.png"
    $personalAccessToken = "YourPAT"
    $filePath = "C:\Users\wade\Downloads\testimage.png"
    
    
    $workItemId = "498"
    
    $uploadUrl = "https://dev.azure.com/$organization/$project/_apis/wit/attachments?fileName=$fileName&api-version=7.1-preview.3"
    
    
    $headers = @{
        Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
    }
    
    $response = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $headers -InFile $filePath -ContentType 'application/octet-stream'
    
    $imageUrl = $response.url
    
    Write-Host The imageurl is: $imageUrl
    
    $description = "<div>add testimage in description<img src='$imageUrl' 'alt=testimage.png'></div>"
    
    $body = @(
        @{
            op = "add"
            path = "/fields/System.Description"
            value = $description
        }
    )
    
    Invoke-RestMethod -Uri $updateUrl -Method Patch -Headers $headers -Body (ConvertTo-Json -InputObject $body) -ContentType 'application/json-patch+json'
    

    You can refer to below similar ticket for your reference:

    How to add inline image in Azure Devops work item description

    pass a valid patch document to update work item using Powershell

    Edit:

    If your source image is external online, you need to store it firstly, change the attachment code as below:

    $organization = "orgname"
    $project = "projectname"
    $fileName = "testimage.png"
    $personalAccessToken = "PAT"
    
    # Define the URL of the image and the path where you want to save it
    $imageUrl = "https://s3.amazonaws.com/movidesk-files/D584934F217A52B0230B0870091B7EFA"
    $filePath = "C:\Users\wade\Downloads\testimage.png"
    
    # Download the image
    Invoke-WebRequest -Uri $imageUrl -OutFile $filePath
    
    
    $uploadUrl = "https://dev.azure.com/$organization/$project/_apis/wit/attachments?fileName=$fileName&api-version=7.1-preview.3"
    
    
    $headers = @{
        Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
    }
    
    $response = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $headers -InFile $filePath -ContentType 'application/octet-stream'
    
    $imageUrl = $response.url
    
    Write-Host The imageurl is: $imageUrl