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.
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.
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
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.