我创建了一个 powershell 表单供用户提交工单请求。我正在尝试使用他们的 api 将其发送到freshdesk。以下是我从各个论坛拼凑的以下代码。我不断收到错误“请求正文的 json 格式无效”
更新:更好地查看代码简单的帮助台表格 任何帮助将不胜感激。
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/'
}