Reputation: 11
I am trying to attach a html file to a test run via rest api using powershell task in my yaml. following is the task that I have used.
inputs:
targetType: 'inline'
script: |
$username = ""
$token = ""
$instance = ""
$teamProjectName = ""
#create auth header to use for REST calls
$accessToken = ("{0}:{1}" -f $username,$token)
$accessToken = [System.Text.Encoding]::UTF8.GetBytes($accessToken)
$accessToken = [System.Convert]::ToBase64String($accessToken)
$headers = @{Authorization=("Basic {0}" -f $accessToken)}
$runId = "12345"
Write-Host $runId
$filePath = "d:\a\1\UIAutomation\report123.html"
$Stream = "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABlSURBVDhP7cxBCsAgDERR739pG/CnGJI0FopQ8O2cjNP6R85QbeNQU7wT1dkijaQ3vkZoWElaoTeJojW01cYh0jwfgiFBV/lEjOZtacijN/nLkOBHhIaVDgn+Wdycp6FXzlCl9wt0Y0cAzHo/zgAAAABJRU5ErkJggg=="
$uri = "https://$instance/$teamProjectName/_apis/test/Runs/$runId/attachments?api-version=5.1-preview.1"
$body = @{
'stream' = $Stream
'fileName' = $filePath
'comment' = 'Test attachment upload'
'attachmentType' = 'GeneralAttachment'
}
$testResult = Invoke-RestMethod -Method Post -ContentType 'application/json' -Headers $headers -Uri $uri -Body (ConvertTo-Json $body -Depth 10)
Write-Host $testResult
I am getting an error in the console stating..
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Invalid filename specified. Filename contains invalid
character ':'.\r\nParameter name: fileName","typeName":"Microsoft.VisualStudio.Services.Common.VssServiceException,
Microsoft.VisualStudio.Services.Common","typeKey":"VssServiceException","errorCode":0,"eventId":3000}
At D:\a_temp\077f26bc-c683-457d-be48-78fe5200d6a4.ps1:32 char:15
... estResult = Invoke-RestMethod -Method Post -ContentType 'application/ ...
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Can someone help me with how to invoke the rest post method for attaching a file to a testrun?
Upvotes: 1
Views: 1239
Reputation: 51183
According to the error, it indicate Invalid filename specified.
From the official tutorial: Attachments - Create Test Run Attachment
filename string Attachment filename
It's just the file name which should not include file path. Suggest you first run the Post API on same 3rd-party tool like postman, check if it works well with Azure DevOps and then move it to powershell script.
You could also take a look at this blog: Azure DevOps Rest API. 17. Add Bugs and Attachments to Test Results which use code to call same Rest API.
Upvotes: 1