1

I created a powershell form for users to submit ticket requests. I'm trying to have it send to freshdesk using their api. Below is the following code I've pieced together from various forums. I keep getting the error "request body has invalid json format"

Update: Better view of code Simple Helpdesk Form Any help would be greatly appreciated.

function sendRequest()
{
# API Key
$FDApiKey="apikey"
#################################################

# Force TLS1.2 as Powershell defaults to TLS 1.0 and Freshdesk will fail 
connections
[Net.ServicePointManager]::SecurityProtocol = 
[Net.SecurityProtocolType]::TLS12

# Prep
$pair = "$($FDApiKey):$($FDApiKey)"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$FDHeaders = @{ Authorization = $basicAuthValue }
##################################################

# The Doing part
#$FDBaseEndpointSummary =  
"https://clasd.freshdesk.com/helpdesk/api/v2/tickets"
#$FDContactData = Invoke-RestMethod -uri $FDBaseEndpointSummary -Headers 
$FDHeaders -Method Post -ContentType application/json 

$ticketArgs = @{
  email = '$email.Text'
  subject = '$subject.Text'
  description = '$description.Text'
  status = '2'
  type = '$request.Text'
  priority = '1'
  } 
 $json =   
     @{email='$email.Text';description='$description.Text'
    ;subject='$subject.Text';status='2';priority='1';type='$request.Text'}
    Invoke-WebRequest -uri 'https://clasd.freshdesk.com/api/v2/tickets' 
   -Headers $FDHeaders -Method Post -Body $json -ContentType 
    application/json
    #Invoke-WebRequest "https://clasd.freshdesk.com/api/v2/tickets/" - 
    Headers $FDHeaders  -ContentType "application/json" -Method Post 
    -Body " 
    { 'description':'$description.Text','email':'$email.Text', 
    'subject':'$subject.Text','type':'$request.Text','priority':'1',
    'status':'2' }" 
    #Invoke-WebRequest -Headers $FDHeaders  
     -ContentType "application/json" -Body "{ 
     'description':'$description.Text','email':'$email.Text',      
     'subject':'$subject.Text','type':'$request.Text','priority':'1',
     'status':'2' }" -method Post 
     'https://clasd.freshdesk.com/api/v2/tickets/'
      }
Justin Merwin
  • 93
  • 2
  • 8
  • 16
  • 1
    If you are trying to input variables to email and the other variables in th hash table you have you may want to enclose them in quotes to get string right from email = '$email.Text' to email = "$($email.Text)" I assume the $email.text is a powershell variable. – Thom Schumacher Apr 13 '18 at 17:23
  • yes it is a powershell variable. I tried what you suggested and still get the error message of invalid json format – Justin Merwin Apr 13 '18 at 17:34

2 Answers2

0

Here is what I meant see script at end of thread.

Is the api you are trying to call: https://developers.freshdesk.com/api/#ticket_attributes

function sendRequest()
{
# API Key
$FDApiKey="apikey"
#################################################

# Force TLS1.2 as Powershell defaults to TLS 1.0 and Freshdesk will fail 
connections
[Net.ServicePointManager]::SecurityProtocol = 
[Net.SecurityProtocolType]::TLS12

# Prep
$pair = "$($FDApiKey):$($FDApiKey)"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$FDHeaders = @{ Authorization = $basicAuthValue }
##################################################

# The Doing part
#$FDBaseEndpointSummary =  
"https://clasd.freshdesk.com/helpdesk/api/v2/tickets"
#$FDContactData = Invoke-RestMethod -uri $FDBaseEndpointSummary -Headers $FDHeaders -Method Post -ContentType application/json 

$ticketArgs = @{
  email = "$(email.Text)"
  subject = "$(subject.Text)"
  description = "$(description.Text)"
  status = '2'
  type = "$($request.Text)"
  priority = '1'
  } 
 $json =   
     @{email="$(email.Text)";description="$(description.Text)"
    ;subject="$(subject.Text)";status='2';priority='1';type="$($request.Text)"}

$jasonPayload = $json | convertto-json

Invoke-WebRequest -uri 'https://clasd.freshdesk.com/api/v2/tickets' -Headers $FDHeaders -Method Post -Body $jsonPayload -ContentType application/json 

#Invoke-WebRequest "https://clasd.freshdesk.com/api/v2/tickets/" -Headers $FDHeaders  -ContentType "application/json" -Method Post -Body "{ 'description':"$(description.Text)",'email':"$(email.Text)",'subject':"$(subject.Text)",'type':'$request.Text','priority':'1','status':'2' }" 

#Invoke-WebRequest -Headers $FDHeaders -ContentType "application/json" -Body "{'description':"$(description.Text)",'email':"$(email.Text)",'subject':"$(subject.Text)",'type':'$request.Text','priority':'1','status':'2' }" -method Post 'https://clasd.freshdesk.com/api/v2/tickets/'
}
Thom Schumacher
  • 1,469
  • 13
  • 24
  • if I put standard strings in the ticketargs then convertto-json it converts to json ok.. are you converting your hashtables to json before sending them to the API? $ticketArgs = @{ email = "test@tes.com" subject = "subject" description = "description" status = '2' type = 'Text' priority = '1' } ; $ticketArgs | ConvertTo-Json ; – Thom Schumacher Apr 13 '18 at 18:54
  • to better explain what I mean I added $jsonPayload = $json | convertto-json – Thom Schumacher Apr 13 '18 at 19:09
0

I was able to solve everything by adding a -Body ($Body | ConvertTo-JSON)

$Body = @{
description = $description.Text
email = $email.Text
subject = $subject.Text
type = $request.Text
priority = 1
status = 2
}

Invoke-WebRequest "https://clasd.freshdesk.com/api/v2/tickets/" -Headers 
$FDHeaders  -ContentType "application/json" -Method Post -Body ($Body | 
ConvertTo-JSON)
Justin Merwin
  • 93
  • 2
  • 8
  • 16