Question

I need to user a PowerShell script to create a Zendesk ticket. But I keep getting the following error back from the API:

The remote server returned an error: (422) Unprocessable Entity.

Below is the script:

$username="username";
$password="password";

$request = [System.Net.WebRequest]::Create('https://domain.zendesk.com/api/v2/tickets.json');
$request.Method = "POST";
$request.ContentType = "Content-Type: application/json";


$request.Credentials = New-Object System.Net.NetworkCredential($username, $password) 


$bytes = [System.Text.Encoding]::ASCII.GetBytes('{"ticket":{"subject":"My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}');

$requestStream = $request.GetRequestStream();
$requestStream.Write( $bytes, 0, $bytes.Length );
$requestStream.Close();

$response = $request.GetResponse();

Thanks in advance for any insights, as I've spend most of my day banging my head against the wall.

Was it helpful?

Solution

Powershell 3 has Invoke-RestMethod which you will find useful, you won't have to worry about creating a validity of your entire call, just the contents part. Also try with square brackets:

[{"ticket":{"subject":"My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top