Solved

How can I create a ticket or add a note to a ticket with an attachment in Powershell

  • 25 January 2022
  • 5 replies
  • 188 views

Badge

Hello dear reader,

I hope you are well and safe

I am trying to add a note to a ticket with an attachment in Powershell but I always receive a 400 HTTP code (bad request)

I’ve already used a lot FreshService’s API and I always managed to make it work (adding note without attachment, creating service requests...).
But this time, attachments are really making me struggling a lot.

I’ve never send an attachment through an API (FreshService or something else) in any programming languages.
I’ve some problem understanding what should I send (type of object ?, path of the file ?, bytesof the files ?) in the body so FreshService’s API can recognize my file and I even tried to achieve that with other method (such as cURL) but I always failed.

If you have any tips or PowerShell code sample to help me, it would be very cool from you 

 

Have a great day and stay safe

Regards

Lucas

icon

Best answer by sanofar.allahpichai 17 February 2022, 19:27

View original

5 replies

Userlevel 7
Badge +12

Hello @Lucas, I would suggest dropping your question in the developer community at https://community.developers.freshworks.com/. I am sure you will find a solution to your issue.

-Zach

Badge

Hello Zach,

Thank you for your answer
I’ll go ahead and follow your tips
I’ll keep this thread updated if I have an helpful answer from the devs

Lucas

Userlevel 3
Badge +6

Hi@Lucas,

 

Here is how you can create a ticket in Freshservice with an attachment:

https://api.freshservice.com/#create_ticket

Sample cURL:

curl -v -u user@yourcompany.com:test -F 'attachments[]=@/Users/user/Desktop/api_attach.png' -F 'subject=Support Needed...' -F 'description=Details about the issue...' -F 'email=tom@outerspace.com' -F 'priority=1' -F 'status=2' -X POST 'https://domain.freshservice.com/api/v2/tickets'

Please follow the guidelines listed below:

  • Only files on your local machine can be attached using API.

  • The Content-Type should always be multipart/form-data for create/update requests with attachments.

  • The name of the file is preserved after it is attached i.e the filename sent in the response will be the same as the one in the request

https://api.freshservice.com/#attachments

 

We hope this helps!

Regards,
Sanofar
Team Freshservice

 

Badge

Hello Sanofar,

Sorry for the delay I was out of office


Unfortunately I am using not using cURL but PowerShell.

However I am unable to translate this cURL sample code into PowerShell working code.
I tried but I am missing something but I can’t find out what …

 

Anyway thank you for trying to help me

 

Regards,
Lucas

Hello,

Please find Powershell version of above command

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic <BASE64 Encoded Api Key>")

$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$multipartFile = '/Users/manojdhadke/Downloads/Temp Download/sample.json'
$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "attachments[]"
$fileHeader.FileName = "sample.json"
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$multipartContent.Add($fileContent)

$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "body"
$stringContent = [System.Net.Http.StringContent]::new("Note content")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "private"
$stringContent = [System.Net.Http.StringContent]::new("true")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$body = $multipartContent

$response = Invoke-RestMethod 'https://<freshservice domain>/api/v2/tickets/<ticket_nemuric_id>/notes' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json


Note: Make required changes as per need

Reply