Create new ticket using PowerShell and API V2

I am attempting to create a new FreshService ticket via PowerShell using API V2. I have successfully created a ticket via PowerShell using API V1. I have also successfully created a ticket via Curl using API V2. However, when creating a ticket via PowerShell using API V2, I get an Error 400 “Bad Request” notice. I have successfully run other API V2 commands via PowerShell.

Test code shown below. All specific data anonymised in **.

$APIKey = "*api-key*"
$EncodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $APIKey,$null)))
$HTTPHeaders = @{}
$HTTPHeaders.Add('Authorization', ("Basic {0}" -f $EncodedCredentials))
$HTTPHeaders.Add('Content-Type', 'application/json')

$URL = "https://*domain*.freshservice.com/api/v2/tickets"

$Attributes = @{}
$Attributes.Add('requester_id',*requesterID*)
$Attributes.Add('subject',"Test subject")
$Attributes.Add('description',"Test description")
$Attributes.Add('group_id',*groupID*)
$Attributes.Add('source',2)
$Attributes.Add('priority',1)
$JSON = $Attributes | ConvertTo-Json
$JSON
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $URL -Headers $HTTPHeaders -Body $JSON -Method Post

Error received

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:19 char:1
+ Invoke-RestMethod -Method POST -Uri $URL -Headers $HTTPHeaders -Body  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

@joshua.tomlinson,

Is this issue that you are seeing specifically happening on PowerShell?

Have you tried making a via Postman or cURL ? Do you see the same error?

1 Like

Hi Saif,

Yes, this error only shows in PowerShell. However, if I restructure the same command for cURL, it works fine.

I have used PowerShell and API V2 to change ticket status, add a note, list all tickets, list all agents. Each one of these works fine. Also, I have successfully used API V1 to create a ticket via PowerShell. It is only this specific scenario (creating a ticket with API V2 and PowerShell) which I can’t get to work. I have checked it over and over again but can’t see what I could be doing wrong!

Is there a way of seeing the log of API calls to my instance of FreshService?

2 Likes

@joshua.tomlinson - Can you share the domain of the freshservice account? Also time range when you saw this requests failing?

Hi Saif,

Is it “safe” to add this information here?

I have initiated a private message to you on this forum. Please check you inbox. :slight_smile:

policeconduct.freshservice.com

I have just run a test now at 12:35 and I am still getting the same error.

1 Like

@joshua.tomlinson,

I have an update to share with you.

Can you please check your inbox in order to proceed further with the discussion?

Solution is that I had missed the “status” field and this is a mandatory field.