Forums

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

Insight API to to upload an Attachment

Dirk Ronsmans
Community Champion
June 16, 2022

Hi!

I've been trying to rack my brain around this but don't seem to be able to find a solution.

I'm trying to use the Insight REST API (https://insight-javadoc.riada.io/insight-javadoc-8.6/insight-rest/#) and more specifically the POST /attachments/object/{objectId} endpoint to send an attachment thru (preferably) Powershell and attach it to an existing object.

The attachment itself is located on disk.

While I have plenty of experience with regular REST API calls with the headers and JSON payload, this one just seems to elude me..

 

Is there anyone that has some better examples that the documentation page or actually has build a Powershell (or Python/some other language) script that will take a local file and attach it to an Insight object?

All help here would be appreciated!

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 25, 2022

I used Postman to create a successful post... then used its code snippet output to show what that would look like in "Powershell - RestMethod"

It looked like this:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization""Basic blahblahblah")
$headers.Add("X-Atlassian-Token", "no-check")

$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$multipartFile = '/C:/path/to/file/filename.ext'
$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "file"
$fileHeader.FileName = "/C:/path/to/file/filename.ext"
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$fileContent.Headers.ContentDisposition = $fileHeader

$multipartContent.Add($fileContent)

$body = $multipartContent

$response = Invoke-RestMethod 'https://jira-base-url/rest/insight/latest/attachments/object/NNN' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

 I couldn't get that to run ... probably something weird with escaping the filename. And I'm not super familiar with powershell.

But I was able to get this to run from a linux command prompt

curl -L -X POST 'https://jira-base-url/rest/insight/latest/attachments/object/NNN'  -H 'X-Atlassian-Token: no-check'  -H 'Authorization: Basic blahblahblah' -F file=@/path/to/file.ext

So the issue with powershell is just about how to propertly create a multi-part form data ody

I was able to hack the following together borrowing from many bits and pieces online:

$LF = "`r`n"
$file = "C:\path\to\file.ext"
$fileName = Split-Path $file -Leaf;
$boundary = [System.Guid]::NewGuid().ToString();
$fileBin = [System.IO.File]::ReadAllBytes($File);
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$fileEnc = $enc.GetString($fileBin)
$bodyLines = @(
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
"Content-Type: application/octet-stream$LF",
$fileEnc,
"--$boundary"
) -join $LF
$Headers = @{
'Authorization' = "Basic yourtokenhere"
'X-Atlassian-Token' = 'no-check'
}
$params = @{
Uri = "https://jira-base-url/rest/insight/latest/attachments/object/NNN"
Body = $bodyLines
Method = 'Post'
Headers = $headers
ContentType = "multipart/form-data; boundary=$boundary"
}
Invoke-RestMethod @params

I don't understand all of it... but it works.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events