Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Upload attachment to Jira API

Scott Holland November 2, 2021

I'm having trouble adding an attachment with the Add-JiraIssueAttachment cmdlet from JiraPS, but this seems to work from a linux machine

curl --location --request POST 'https://company.atlassian.net/rest/api/3/issue/TEST-1234/attachments' -u 'Myemail@company.com:THEAPIKEYTHATIUSE' -H 'X-Atlassian-Token: no-check' --form 'file=@"c:\temp\csvwithstuff.csv"'

so if I go native api in my script, how to I migrate this curl command to invoke-restmethod? Here's what I have but it's not working

$Token = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Myemail@company.com:THEAPIKEYTHATIUSE'))
$auth = @{
            'authorization' = "Basic $Token"
            'X-Atlassian-Token' =  'no-check'
        }

$file = "c:\temp\csvwithstuff.csv"
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$boundary = [System.Guid]::NewGuid().ToString()
$readFile = [System.IO.File]::ReadAllBytes($file)
$fileEnc = $enc.GetString($readFile)


$url = "https://company.atlassian.net/rest/api/3/issue/TEST-1234/attachments"

Invoke-restmethod -Uri $url -Headers $auth -Method POST -ContentType "multipart/form-data; boundary=`"$boundary`"" -InFile $filePath -Verbose 

But i'm getting an error message about

Invoke-restmethod : Cannot find drive. A drive with the name <contents of the file>

What am I doing wrong?

1 answer

1 vote
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 4, 2021

Hi Scott,

I understand that you're using powershell and want to try to upload an attachment to an issue in Jira Cloud.  It seems like you have previously tried to use the JiraPS library, but right now are trying to upload a file without using that library and just the native powershell client.

I came across another post over in https://community.atlassian.com/t5/Confluence-questions/How-to-Upload-an-Attachment-to-Confluence-Page-using-PowerShell/qaq-p/631469 where a user was trying to do the same, but for Confluence.  JJ found that the invoke-restmethod used by powershell isn't playing nicely with the multipart-form content header that Jira is expecting to use with other REST clients.  In turn he found another way to add an attachment using powershell. 

I have adapted his response for Jira Cloud.  I tested this and it does appear to work, although when I tested this I manually substituted the $authToken in line 15 with my own base64 encoded string containing email:APIToken as described in Basic auth for REST APIs but otherwise the code should be the same.  You will need to update for your site URL, issuekey, and file path in addition to the authorization credentials to get this working, but I expect it should help.

 

function Add-AttachmentToIssue { 
[CmdletBinding()]
param (
[string]$baseUrl, #base url to create the api uri from, no trailing slash
[string]$issueKey, #id of the issue in Jira
[string]$filePath, #path to the file to upload
[string]$authToken #base64 encoded string of username:APItoken
)

begin {
Write-Host "creating uri and web client for file upload"
$uri = "$baseUrl/rest/api/3/issue/$issueKey/attachments";
$webClient = New-Object System.Net.WebClient;
$webClient.Headers.Add('X-Atlassian-Token','no-check');
$webClient.Headers.Add('Authorization',"Basic $authToken");
}

process {
Write-Host "Uploading $filePath Jira issue $issueKey via uri $uri";
$webClient.UploadFile($uri,$filePath);
}

end {
Write-Host "$filePath attached to Jira issue id $issueKey";
return;
}
}

function Convert-StringToBase64 {
[CmdletBinding()]
param ( $Text )
begin { $Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text) }
process { $EncodedText =[System.Convert]::ToBase64String($Bytes) }
end { return $EncodedText }
}

$authToken = Convert-StringToBase64 -Text 'emailaddress:APItoken';

Add-AttachmentToIssue -baseUrl 'https://[YOURCLOUDSITE].atlassian.net' -issueKey SCRUM-34 -filePath 'c:\temp\eula.txt' -authToken $authToken;

Try this and let me know if you run into any problems.

Andy

Hari May 29, 2024

getting "403 forbidden" error, on trying the above code

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events