Help to identify JIRA REST API issue using powershell

Kim Kongsted August 2, 2021

Hi,

 

I really hope somebody can help me understand why i'm suddently unable to upload files via the rest API. I can create issues, I can extract them, But adding attachments are not working.

The following part of my code is not working for some reason:

Invoke-RestMethod -Headers $jira_headers -Uri $uri -Method POST -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $body

The server is responding with the following message: 

Object reference not set to an instance of an object.

 

Here is the code that is being used (variables, like strings have all be tested and the values are fine):

[string] $Filename = $rowA.FILE_NAME
[string] $templatePath = "C:\UK\JIRA_upload\FILES\" + $rowA.ID + "\"

$path = $templatePath + $rowA.FILE_NAME
if(!(Test-Path $templatePath))
{
New-Item -ItemType Directory -path $templatePath
}
[System.IO.File]::AppendAllText($path, $rowA.FILE_CONTENT)

$jira_headers = @{
"Authorization" = $basicAuth
"X-Atlassian-Token" = "nocheck"
}


$uri = "$target/rest/api/2/issue/$jid/attachments"
$myfile = $path

$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=`"$Filename`"",
"Content-Type: application/octet-stream$LF",
$fileEnc,
"--$boundary--$LF"
) -join $LF


$serverResponse = Invoke-RestMethod -Headers $jira_headers -Uri $uri -Method POST -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $body

 

1 answer

0 votes
Kim Kongsted August 20, 2021

Found a way around the issue

Antoine Delorme August 3, 2023

So what was the solution ?

Suggest an answer

Log in or Sign up to answer