Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,366,270
Community Members
 
Community Events
168
Community Groups

How to attach files to Jira Ticket via PowerShell

Edited

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

2 answers

1 accepted

1 vote
Answer accepted

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

Im trying to upload image/jpg or image/png do you know how i can do that?

0 votes
morellana I'm New Here Aug 19, 2022

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.


function Add-RwJiraIssueAttachment {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        $IssueKey,
        [Parameter(Mandatory = $true)]
        [System.Type.Uri]
        $FileLocation
    )

    begin {
        try {
            #use Invoke-Restmethod instead of Invoke-webrequest -- RestMethod returns the appropriate string type and you dont have to deal with conversion of the data
            $readFile = Invoke-RestMethod $FileLocation
        }
        catch {
            Write-Error "Could not retrieve file from url"
            break
        }

        #get file name from uri
        $fileName = $FileLocation.Split('/')[-1]
        $boundary = (New-Guid).Guid
        #Do not forget this line -- was troubleshooting for 3 days without this
        $LF = "`r`n";

    }# end begin

    process {
        #construct body to set content disposition
        #using Invoke-RestMethod's form parameter is a lost cause as it does not support nested values in form data.
        $bodyLines = @'
--{0}
Content-Disposition: form-data; name="file"; filename="{1}"
Content-Type: application/octet-stream{3}
{2}
--{0}--{3}
'@ -f $boundary, $fileName, $readFile, $LF
        #construct splat
        $attachment_splat = @{
            Method      = "POST"
            Uri         = ($env:JIRA_SANDBOX + "issue/{0}/attachments") -f $IssueKey
            Headers     = @{
                "Authorization"     = "Basic $($env:JIRA_KEY)"
                'X-Atlassian-Token' = 'no-check'
                "Accept"            = "application/json"
            }
            body        = $bodyLines
            #content type must remain multipart/form-data with boundary definition
            ContentType = "multipart/form-data; boundary=`"$boundary`""
        }
        #upload the file
        $request = Invoke-RestMethod @attachment_splat
        $request
    }# end process

}








Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events