Windows – Invoke-RestMethod returns 400 error no matter what I do

automationhelpdeskpowershellscriptingwindows

So I'm working on a helpdesk automation with the Samanage API and need to update the "state" of the ticket. I've read the documentation and there is nothing i saw for updating the ticket state. My Code is below:

    import-module dbatools

# Import API Config from user's profile
$config = Get-Content ~\.samanage\creds.json | ConvertFrom-Json


#Dictionary of headers
$headers = @{
    'Accept' = 'application/vnd.samanage.v2.1+json'
    'Content-Type' = 'application/json'
    'X-Samanage-Authorization'= "Bearer $($config.api_key)"
}
 
#Now how do we find incidences assigned to this user?
$all_alfreds_tickets = Invoke-RestMethod -Method "GET" -URI "https://api.samanage.com/incidents.json?assigned_to=6671937" -Headers $headers

# loop through all the tickets that came back from our API Call
$all_alfreds_tickets| foreach-object -process {

    $ticket = $_

    # Do thing based on ticket category
    switch ($_.subcategory[0].name){

        # How does Alfred handle a Workstation exception request?
        ('Alfred-Workstation Exceptions'){

            #
            # Check Ticket status, if state != Request work the ticket
            #

            # Move the computer into the workstation exceptions out 
            Get-ADComputer $ticket.name | Move-ADObject -TargetPath <Path to OU>

            
            # Respond to the tick letting the world know what's been done.
            Invoke-RestMethod -Method POST -URI "https://api.samanage.com/incidents/$($ticket.id)/comments.json" -headers $headers -Body '{"body": "Hello, I have moved this computer to the Workstation Exceptions OU", "State": "In-Progress",}'
            
            # Update the ticket status to "In-Progress"
            Invoke-RestMethod -Method Put -URI "https://api.samanage.com/incidents/$($ticket.id).json -headers $headers -body '{"state":"In-Progress"}'## Heading ##


            # Add the ticket to the database for monitoring.
        
        }
    }
}

My trouble is no matter what I do I cannot get the Invoke-RestMethod to work successfully. I was beginning to think maybe it just wasn't supported but my manager got it to work but isn't really being forth coming with guidance or suggestions, so there's a way to do it, i just dont know what it is.

s best I can tell I think the problem might by with my body but I'm not sure why that would be because I'm doing the same thing one line of code above and it's working just fine.

I've tried a couple different methods:

Put: Invoke-RestMethod returns "400, Bad Request"

Post: Invoke-RestMethod returns " 404, Not Found"

Patch: Invoke-RestMethod returns "403, forbidden"

and I've played with changing how the body is put together.

any help or guidance would be much appreciated.

Best Answer

I should have read the documentation slower.

You have to send an instance of an incident as a JSON object not just the properties you want to update from the incident.