cancel
Showing results for 
Search instead for 
Did you mean: 

Upload Attachments using Rest API

dhilip_ind
Contributor III

Hi folks,

 

I have some issues with the Base64 encoded data, I was able to successfully upload the attachment to the archer record, however the Issue is with the attached files in the archer record.The Attached files are always getting corrupted and not coming exactly what I encoded. 

 

Here is my code: 

 

$path = "C:\Attachments\Test.pdf"
$content = Get-Content -Path $path
#$content
$bytes = [System.Text.Encoding]::UTF8.GetBytes($content)
$base64 = [System.Convert]::ToBase64String($bytes)
$base64
$body_upload = @"
{
`"AttachmentName`": `"Test.pdf`",
`"AttachmentBytes`": `"$base64`"
}
"@
$results = Invoke-RestMethod -Method POST -Uri $api_url -Headers $headers -Body $body_upload -ContentType "application/json"

if ($results.IsSuccessful -and $results.ValidationMessages.count -eq 0) {
$results.RequestedObject
$File_ID = $results.RequestedObject.Id
# "File ID = " + $File_ID
}
else {
$results.ValidationMessages
}


$file_body = "{
`"Content`": {
`"Id`": 462209,
`"LevelId`": 62,
`"Tag`": `"Findings`",
`"FieldContents`": {
`"16597`": {
`"Type`": 11,
`"Tag`": `"Attachments`",
`"Value`": [$File_ID],
`"FieldId`": 16597
}
}
}
}"

$response = Invoke-RestMethod -Method PUT -Uri $contentupdate_url -Headers $headers -Body $file_body -ContentType "application/json"
1 ACCEPTED SOLUTION

Accepted Solutions

Thanks David. 

 

Found the issue. 

 

Changed 2nd line and it's working. 

$content = Get-Content -raw $path

View solution in original post

3 REPLIES 3

DavidPetty
Archer Employee
Archer Employee

I wonder if the UTF8.GetBytes is causing the issue.

 Advisory Consultant - Archer, an RSA Business

Thanks David. 

 

Found the issue. 

 

Changed 2nd line and it's working. 

$content = Get-Content -raw $path

Does  the attachment work with a user account only has the application access and the record permission? or does it need a sysadmin access to attach a file through an API?