Upload an attachment to Azure DevOps via HTTP POST requestU

Im using n8n to create an integration Movidesk/AzureDevOps.

Im trying to get the attachments people put in the Movidesk to an AzureDevOps workitem, but cant figure out how to upload the attachment in the first place. The part of linkink it to a workitem I manage to succeded.

Since im using, i cant download anything in my local computer, so what i have to work with in this situation is just:

"attachments":[ 
        { 
          "fileName":"minhaImagem.png",
          "path":"7BDEA1B62A8641FF86982D0CF9F3DEC0",
          "createdBy":{ 
            "id":"CodRefDeQuemEnviouOArquivo",
            "personType":1,
            "profileType":1,
            "businessName":"Nome de quem enviou o arquivo",
            "email":"[email protected]",
            "phone":"(47) 99999-9999"
          },
          "createdDate":"2017-05-29T14:19:50.0129141"
        }
      ],

But i dont know how to use the “path” variable.

For

I used this post api call:

  1. POST https://dev.azure.com/%7BmyOrg%7D/%7BmyProject%7D/_apis/wit/attachments?fileName=test.xlsx&api-version=7.1-preview.3

It returns me an id and a url as it should, but when i open it it has no content.

I assumed it was a problem with the body of the API.Its missing something the content itself of the file, but i didnt figured it out how to use it with only the fileName and the path

in the work item, it stays like this. If i try downloading it, the file returns me an error that the file is corrupted

To upload an attachment to Azure DevOps and link the attachment to a work item via the REST API request, please follow the following steps:

  1. Convert file content to binary data. The sample PowerShell script:
$fileName = "123.png" # replace with your file name and file name extension
$filePath = "C:UsersxxxxPicturesScreenshots"+$fileName # replace with your file path
$fileContent = [System.IO.File]::ReadAllBytes($filePath)
  1. Upload the binary file to Azure DevOps as attachment.
POST https://dev.azure.com/xxxxx/_apis/wit/attachments?fileName=fileName.png&api-version=7.2-preview.4

You will get an id and a url as return. We will use the url from the response.
Example response:

{
  "id": "a5cedde4-2dd5-4fcf-befe-fd0977dd3433",
  "url": "https://dev.azure.com/xxxxx/_apis/wit/attachments/a5cedde4-2dd5-4fcf-befe-fd0977dd3433?fileName=fileName.png"
}
  1. Add an attachment to the workitem.
PATCH https://dev.azure.com/xxxxx/_apis/wit/workitems/{id}?api-version=7.2-preview.3

[
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      "url": "https://dev.azure.com/xxxxx/_apis/wit/attachments/a5cedde4-2dd5-4fcf-befe-fd0977dd3433?fileName=fileName.png",
      "attributes": {
        "comment": "Attachment uploaded"
      }
    }
  }
]

Here is the full sample PowerShell script:

$organization = ""
$project = ""
$personalAccessToken=""

$fileName = "123.png" # replace with your file name and file name extension
$filePath = "C:UsersxxxxPicturesScreenshots"+$fileName # replace with your file path
$workItemId = "583"  # Replace with your work item ID

# Convert file content to binary data
$fileContent = [System.IO.File]::ReadAllBytes($filePath)

# Upload the file to Azure DevOps
$uri = "https://dev.azure.com/$organization/_apis/wit/attachments?fileName=$fileName&api-version=7.2-preview.4"  
$headers = @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
}

$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $fileContent -ContentType  "application/octet-stream"
$attachmentUrl = $response.url

# Link the attachment to the work item
$uri = "https://dev.azure.com/$organization/_apis/wit/workitems/$($workItemId)?api-version=7.2-preview.3"
$body = @"
[
    {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "AttachedFile",
            "url": "$attachmentUrl",
            "attributes": {
                "comment": "Attachment uploaded via PowerShell script"
            }
        }
    }
]
"@

$response = Invoke-RestMethod -Uri $uri -Method Patch -Headers $headers -Body $body -ContentType "application/json-patch+json"

Test result:

I can download these files and read them successfully.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật