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,296,828
Community Members
 
Community Events
165
Community Groups

Insight API to to upload an Attachment

Dirk Ronsmans Community Leader Jun 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

0 votes

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
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

330 views 0 10
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you