Description field throws error while creating a ticket through the V2 API

Hello everyone,


I'm stuck with an issue. I made a script that will export emails from an Outlook folder then for each conversation it will create a ticket.


I'm using Powershell and the following code :


    

# Outlook Variables
$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder(6)

# Get the emails that are in the folder named "folder_name"
$targetfolder = $inbox.Folders | where-object { $_.name -eq "folder_name" }

$items_list = $targetfolder.items | Select-Object ConversationID, sentOn, SenderName, To, TaskSubject, htmlbody, Attachments | sort-object -Property ConversationID, @{Expression = { $_.sentOn }; Ascending = $false } | select -First 1

foreach($item in $items_list){
    $item.Body
}

# Fresh's Part
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

$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')

foreach ($item in $items_list) {
    $Body = @{
        description = $item.htmlbody
        email       = "requester_email"
        subject     = $item.TaskSubject;
        priority    = 1;
        status      = 2
    }

    # Create the ticket
    $URL = 'https://lundin.freshservice.com/api/v2/tickets'
    Invoke-RestMethod -Method Post -Uri $URL -Headers $HTTPHeaders -Body ($Body | ConvertTo-Json)
    break
} 

I get the following error : Invoke-RestMethod : {"code":"invalid_json","message":"Request body has invalid json format"}


This only happens when I try to add a description from the variables. I thought that it had something to do about how the HTML Body ($item.htmlbody) of the email was being exported but the error happens with the Raw body ($item.body) of the email which doesn't contain any HTML.


We're trying to make tickets to have a track of the issues that happened with a particular software.

Login or Signup to post a comment