Rest api problems

I am trying to access an api and create a new ticket on a servicedesk system. I keep getting error 422 unprocessable entry. The supplier said I could access the body of the reply to find out why but I cannot figure out how to do this.

`$obj.tickets = @{

“subject” = ‘test’
“number” = ‘99999999999’
“created_at” = ‘2015-08-02T13:18:12.680-04:00’
“customer_id” = ‘2486371’
“due_date” = ‘2015-08-15T13:18:12.680-04:00’
“start_at” = ‘2015-08-02T13:18:12.680-04:00’
“end_at” = ‘2015-08-15T13:18:12.680-04:00’
“location_id” = ‘1’
“status” = ‘Open’
“user_id” = ‘3030’

}

$json = $obj | ConvertTo-Json

#Api field information.
#ticket: :number, :subject, :created_at, :customer_id, :due_date,

:start_at, :end_at, :location_id, :problem_type, :status, :user_id

Invoke-RestMethod -Method Post -Uri “http://suffolkcomputerconsultants.repairshopr.com/api/v1/tickets?api_key='xxx-xxxx” -Body $json -ContentType “application/json” `

Any ideas?

You need to capture the result of invoke-webrequest in a variable; pipe that to get-member to see its properties. That should let you more easily access reply headers and body information.

I tried that earlier but it seems to return nothing, just an error.

$var = Invoke-WebRequest -Method Post -Uri “http://suffolkcomputerconsultants.repairshopr.com/api/v1/tickets?api_key=afcc431d-6bd3-4758-ad28-135e8b9d0793” -Body $json -ContentType “application/json”

$var | gm

Any other ideas that I can try?

Invoke-WebRequest : The remote server returned an error: (422) Unprocessable Entity.
At line:1 char:8

  • $var = Invoke-WebRequest -Method Post -Uri "http://suffolkcomputerconsultants.re
  •   + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
    
    

gm : You must specify an object for the Get-Member cmdlet.
At line:3 char:8

  • $var | gm
  •    ~~
    
    • CategoryInfo : CloseError: (:slight_smile: [Get-Member], InvalidOperationException
    • FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

Without documentation from the vendor regarding the API it’s hard to troubleshoot. Some things I noticed were the backtick in front of $obj, why is that there? Also, you are referencing a property (.tickets) of $obj? Your posted code didn’t show it, but my assumption is that you are generating a generic object somewhere above. The question is how is the JSON formatted in the documentation? Again, it’s an assumption, but if there is a “tickets” category (plural), then there would be a ticket child to represent each ticket in tickets, something like this

$obj = @{
    tickets =@{
        ticket=@{
            "subject" = 'test'
            "number" = '99999999999'
            "created_at" = '2015-08-02T13:18:12.680-04:00'
            "customer_id" = '2486371'
            "due_date" = '2015-08-15T13:18:12.680-04:00'
            "start_at" = '2015-08-02T13:18:12.680-04:00'
            "end_at" = '2015-08-15T13:18:12.680-04:00'
            "location_id" = '1'
            "status" = 'Open'
            "user_id" = '3030'
        }
    }
}

$json = $obj | ConvertTo-Json
$json

Which would produce the following JSON:

{
    "tickets":  {
                    "ticket":  {
                                   "start_at":  "2015-08-02T13:18:12.680-04:00",
                                   "number":  "99999999999",
                                   "created_at":  "2015-08-02T13:18:12.680-04:00",
                                   "user_id":  "3030",
                                   "customer_id":  "2486371",
                                   "subject":  "test",
                                   "end_at":  "2015-08-15T13:18:12.680-04:00",
                                   "location_id":  "1",
                                   "status":  "Open",
                                   "due_date":  "2015-08-15T13:18:12.680-04:00"
                               }
                }
}

Thank for for such a detailed answer Rob. The main problems I have at the moment is that there is no json structure in the documentation just a list of valid field names. I have managed to use a rest api test application to get a working json object via testing. This is shown below. It seems the ticket part is not needed all of the fields are assigned to the ticket object by default. The issue is the other fields body and subject need to be assigned to the comments property and I cannot get this to work through powershell. If I try to create a json with this structure in powershell I get a message back saying comments.body cannot be empty.

{
“customer_id”:“2691280”;
“comments”: {“body”: “test@contoso.com”, “subject”: “test” }
}