You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
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!
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.