Problems creating tickets with Powershell and API V2, using Icelandic characters

Good day. I’m testing out how I can create tickets with the Freshservice API, using Powershell. I’m getting a 400 Bad Request error every time I try including characters like Þ, Æ, and Ö, in the description or subject field. If I remove these characters, the tickets are successfully created without any issues.

Has anyone encountered similar problems and found ways to resolve it?

Here are examples of tests I’ve done that have all failed:

$headers = @{
    "Authorization" = "Basic $base64AuthInfo"
    "Content-Type" = "application/json" #I've also tried specifying charset=utf-8
}

$body = @{
    description = "Test with Icelandic characters: Þ, Æ, Ö"
    subject = "Test Þema"
    email = "<test@email>"
    priority = 1
    status = 2
} | ConvertTo-Json -Depth 10 -Compress

$jsonBody  = @"
{
    "description" : "Test with Icelandic characters: Þ, Æ, Ö",
    "subject" : "Test Þema",
    "email" : "<test@email>",
    "priority" : 1,
    "status" : 2
}
"@

$utf8Body = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::UTF8.GetBytes($body))

Write-Output "Sending request with json body: $jsonBody"
Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $jsonBody
Write-Output "Sending request with json body: $body"
Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body
Write-Output "Sending request with json body: $utf8Body"
Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $utf8Body

The error outputted is always the same:

Response Body: {"description":"Validation failed","errors":[{"field":"status","message":"It should be one of these values: '2,6,3,7,4,5,8'","code":"missing_field"},{"field":"subject","message":"It should be of type String","code":"missing_field"},{"field":"priority","message":"It should be one of these values: '1,2,3,4'","code":"missing_field"},{"field":"requester_id","message":"One of the following is mandatory: requester_id, phone, email","code":"missing_field"}]}```

Hey @Einar,
Welcome to the Freshworks Developer Community! :tada:

I’ve shared this with the Freshservice team, and they will help resolve this.

1 Like

Hi @zach_jones_noel and thanks for that sharing this with support + the link.

Support was indeed helpful and helped me stumble upon a solution while waiting for them to escalate my issue.

I tried testing creating a ticket via Postman and found that it could create them with Icelandic characters without any issues.

I then tried switching from the Invoke-RestMethod function to System.Net.WebClient. Once I did that, I got no errors.

Here’s an example code that worked for me:

$uri = 'https://mycompany.freshservice.com/api/v2/tickets'

$body = @{
    description = "<div>Test with Icelandic characters:<br/><br/> Þ, Æ, Ö</div>"
    subject = "Test Þema"
    email = "myemail@mycompany.com"
    priority = 1
    status = 2
} | ConvertTo-Json -Depth 10 -Compress

$webClient = New-Object System.Net.WebClient
$webClient.Headers.Add("Content-Type", "application/json;charset=utf-8")
$webClient.Headers.Add("Authorization", "Basic $base64AuthInfo")
$webClient.Encoding = [System.Text.Encoding]::UTF8

try {
    $response = $webClient.UploadString($uri, "POST", $body)
    Write-Output "Ticket created successfully: $response"
} catch {
    Write-Error "Error creating ticket: $($_.Exception.Message)"
}
1 Like

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.