Hi,
The following curl command attaches a file to Jira ticket without a problem:
curl -D- -u jira:password -X POST -H "X-Atlassian-Token: no-check" -F "file=@myfile.txt" http://jira.mydomain.com:8080/rest/api/2/issue/SO-1/attachments
However, trying to perform the same task using PowerShell always fails:
Invoke-RestMethod -Headers $jira_headers -Uri $uri -Method POST -InFile $myfile
Tried all possible header options including:
"Authorization" = $basicAuth <--- this is not a problem (works otherwise)
'X-Atlassian-Token' = 'nocheck'
'Content-Type' = 'multipart/form-data'
and also using
[Net.ServicePointManager]::SecurityProtocol = ([Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12)
All other features via PowerShell work (download files, update Jira ticket), but for some reason File upload will not work (using PowerShell 5.1).
Any ideas would be highly appreciated.
Best regards,
Nik
Managed to figure it out based on Jira module https://github.com/AtlassianPS/JiraPS and the following stackoverflow article:
https://stackoverflow.com/questions/36268925/powershell-invoke-restmethod-multipart-form-data
Headers:
$jira_headers = @{
"Authorization" = $basicAuth
"X-Atlassian-Token" = "nocheck" <--- Does NOT work without this line
}
URI and FILE:
$uri = "http://jira.mydomain.com:8080/rest/api/2/issue/SO-2/attachments"
$myfile = "C:\Users\myuser\Desktop\testing.txt"
$fileBytes = [System.IO.File]::ReadAllBytes($myfile);
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary = [guid]::NewGuid().ToString()
$LF = "`r`n";
$body = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"testing.txt`"",
"Content-Type: application/octet-stream$LF",
$fileEnc,
"--$boundary--$LF"
) -join $LF
Invoke-RestMethod -Headers $jira_headers -Uri $uri -Method POST -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $body
For anyone who comes across this and is wondering how to upload an image to OR trying to retrieve a file from a URL and attach it to a Jira issue in PowerShell (I know, very unique use cases). This is the solution I was able to uncover.
I am using PowerShell 7.2.6 at time of writing.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.