Invoke-RestMethod Error: Cannot Send Body Type

Justin Merwin picture Justin Merwin · Apr 18, 2018 · Viewed 7.7k times · Source

I am trying to GET tickets from Freshdesk using the Invoke-RestMethod. The error I'm receiving is "Invoke-RestMethod: Cannot send a content-body with this verb type". My function looks like this:

function opentickets()
{
$Body = @{
#description = $description.Text
email = $email.Text
#subject = $subject.Text
#type = $request.Text
#priority = 1
#status = 2
}
Invoke-RestMethod -uri "https://clasd.freshdesk.com/api/v2/tickets?" - 
ContentType application/json -Method Get -Body ($Body | ConvertTo-Json)
}

The Freshdesk API endpoint to view tickets should return something like the examples that freshdesk gives using curl:

https://clasd.freshdesk.com/api/v2/tickets?email=[requester_email]

Update If I use the following:

Invoke-RestMethod -uri 
"https://clasd.freshdesk.com/api/v2/tickets? 
[email protected]" -Headers $FDHeaders -ContentType 
 application/json -Method Get 

I receive the desired outcome. Can I output the result from the Invoke command to something more readable for a user?

Answer

srbrills picture srbrills · Apr 18, 2018

For any client, you cannot send a payload (body) with a GET request to a RESTful service. It used to be that most clients allowed you to do so, but this has since been changed.

Your workaround helps remediate the issue by using query strings instead. You'll have to look at Freshdesk's documentation to see if they support a POST request for retrieving ticket information, or make sure the query-string approach will suit your needs.

Looks like the filtering tickets by query method is what is recommended:

https://developers.freshdesk.com/api/#filter_tickets