0

我创建了一个 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/'
      }
4

2 回答 2

0

我能够通过添加 -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)
于 2018-04-16T19:32:31.500 回答
0

这就是我的意思,在线程末尾看到脚本。

是您尝试调用的 api: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/'
}
于 2018-04-13T18:40:34.200 回答