REST API Attachment

  • 19 June 2018
  • 9 replies
  • 374 views

Hey everyone,

Can anyone tell me please how can I make a attachment request through REST API?

I am using the /sr/{id}/attachment url to add a new attachment to a service record.

Thanks in advance.

9 replies

Same here please.
Has there been any update on this? I'm having the same problem.
I have attached a screenshot of how I was able to do it using Postman.

Here are the highlights.
URL: /sr/{id}/attachment

Add a header:
Key: Content-Disposition
Value: multipart/form-data

The request body needs to be set to "form-data"
Add a file to the body using the below.
Key: file
Value: [attached the file here]
Content Type: The Content Type that matches your file.

Hope that helps!
JHamblen
I have attached a screenshot of how I was able to do it using Postman.

Here are the highlights.
URL: /sr/{id}/attachment

Add a header:
Key: Content-Disposition
Value: multipart/form-data

The request body needs to be set to "form-data"
Add a file to the body using the below.
Key: file
Value: [attached the file here]
Content Type: The Content Type that matches your file.

Hope that helps!


Hi JHamblen!

You solved my problem! It worked!

Thank you, very very much!
Is there a way to instead Get attachments via the API that is retrieve attachments related to an SR Record?
I can't find that anywhere.
I would like to ask if you have already been able to retrieve the attachment file from the Service Desk directory using RESP API?
I would like to ask how can I retrieve the attached file from Service Directory? I haven't found how I can use the REST API to retrieve the attachment file.
Userlevel 5
Badge
Hi JPires,
Hope you already figured it out but if not then here is a step by step guide:

Step 1 - Build URL
In order to add Attachments to a Service Record using RestAPI, you create need to use the POST method.
The URL in the API tool (e.g: Postman) should be for example:
https://accountID.sysaidit.com/api/v1/sr/123456/attachment
(replace the accountID and 123456 with the actual SR number)

Step 2 - populate on postman (or your chosen API builder)
In the "Body" select "form-data" radio button.
Under "KEY" you will have an option to select "FILE" and when you select that option, you will have in the "Type" column an option to select a file.
After the file (Attachment) is uploaded from the computer, then type file under KEY, and click Send button to use the POST method and successfully upload the attachment.


Step 3 - Validate input
You will see "200 OK" for confirmation.

Hope this helped let us know how it goes!
Cheers,
Maayan

JPires
I would like to ask how can I retrieve the attached file from Service Directory? I haven't found how I can use the REST API to retrieve the attachment file.
Because I found this post and had to translate in to PowerShell here is my function I use to post Attachments.

Hope it helps someone.


function Add-SysAidAPISRAttachment{
<#
.INPUTS
Attachment, URI, WebSession, ID
.PARAMETER Attachment - [string] Path including file name
.PARAMETER MediaType
.PARAMETER URI/URL - SysAid URL/URI "http://mysysaid.domain.ca:8080/"
.PARAMETER WebSession - Should be the session from the Get-SysAidAPIKey function
.PARAMETER srID - INT32 id number of the Service Request to add the attachment to
.OUTPUTS
PSCustomObject from returned SysAid Json.
.EXAMPLE
$returnedSR = Get-SysAidAPISR -ServiceRequest $SR -URI "http://mysysaid.domain.ca:8080/" -APIWebSession $webSession
Payload - File Multipart/Form data.
#>
[CmdletBinding(DefaultParameterSetName="Default")]
param (
[Parameter(
ParameterSetName="Default",
Mandatory=$true,
HelpMessage = "Web Session returned ay Get-SysAidAPIKey")]
[Alias("API","JSESSIONID","Key","websession")]
[ValidateNotNullOrEmpty()]
[Microsoft.Powershell.Commands.WebRequestSession]$APIWebSession,
[Parameter(
ParameterSetName="Default",
Mandatory=$true,
HelpMessage="URL/URI for the SysAid Site http://[fqdn]:[port]/"
)]
[ValidateNotNullOrEmpty()]
[Alias("URL","site")]
[String]$URI,
[Parameter(
ParameterSetName="Default",
Mandatory=$true,
HelpMessage="ID - INT32 of the ticket to update"
)]
[ValidateNotNull()]
[Alias("ID","serviceRequestID")]
[Int32]$srID,
[Parameter(
ParameterSetName="Default",
Mandatory=$true,
HelpMessage="File path and name to attach to Service Request"
)]
[ValidateNotNullOrEmpty()]
[Alias("file")]
[string]$attachment,
[Parameter(
ParameterSetName="Default",
HelpMessage="Media Type i.e. image/png"
)]
[ValidateNotNullOrEmpty()]
[Alias("media")]
[string]$mediaType
)

begin{
#Service Request URI's
<#
Content-Disposition: form-data; name="file"; filename="694-117374.wav"
Content-Type: audio/wav
#>
$finalURI= $URI + "api/v1/sr/" + $srID.ToString() + "/attachment"

$body = $null
$fileName = Get-Item $attachment
if ($null -ne $mediaType) {
$mimeType = $mediaType
}else{
$mimeType ="application/octet-stream"
}
$file = [System.IO.File]::ReadAllBytes($attachment)
$fileEnc = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString($file)
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"

$body = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"$($fileName.Name)`"",
"Content-Type: $mimeType+$LF") + $fileEnc + (
"--$boundary--$LF"
) -join $LF

$Response = $null
$functionError = $null
}

process{

try {
$Response = Invoke-RestMethod -Uri $finalURI -Method Post -ResponseHeadersVariable "responseHeaders" -WebSession $APIWebSession -ContentType "multipart/form-data; boundary=$boundary" -Body $body
}
catch {
$functionError = $_
}

}

end{
if ($null -eq $functionError) {
#$Response | ConvertFrom-Json
$Response | ConvertTo-Json
}else{
$functionError
}
}
}

Reply