Trying to understand the error message

I have an application with the following API information. Its giving me a 200 https status and then a 400 status immediately and I dont know what is going on.

This is the error message

0:00:22 : Halo Desktop App Launched
20:00:24 : URL: https://halo.sondelaconsulting.com/auth/token?tenant=sondella
         : HTTP Status: 200
         : Received Content: {"token_type":"Bearer","access_token":"CfDJ8OWiF1jv5RZNsD-A4dveNXjncE7UBiuII0uyBtPGHj6PdHIFIxXp3FdBohBHggIsY2waD_-ob8QizozQQQh_Mo4x9xDk_xgQ-vQYOXPep0p2nOjcAQwAmn7PITFx3o2dKwtX7ZQnVyPAU4IwWN7EeJLjk-OgKl2itszIyQ3L0nTRbU4fWsu88CYk63f59mo42euxwcXmF_ZlqrEx1VNrB4M3oLR2rr4iCZbEUxe0Wund3XmWr5MZuc6a7Ta0cJ7L8a5ecXk8mTDgHfwuM0Qm4wzlfdqC_oQb0c9rBGI6m4J2O8jmeWWN4vx8FTvcy87KSuyL1rUHYFr42rcUI93kmbFAFFr-v8Ab253_GWtKPjAfvy7r-UgJ6rwt0Jv6FxMqfaHna2d3aPRseumn4xiCkLqWuGpwlVPiyVwSw5scZpXUclc_uE_GG7xZ0vbX9uxbtkF4pZ6Qu8AhJk3qfROm9gZ_BtMYdh1czNiQsGZOq3d14QaG64JP6zBisKcOSxqUKjJq3MxMJGhD8-M2mzxfEnrs_uJd4dPDKWKyXAxGq7W2G4rk39OgXJP6VbjMAB4VRarxpCoN9OMQO8hvkVVF9WGJ73HMi-lu6mUyHETxPEZWEfH88BwtZ7AIiEtAK-lFSqUaNJh9BsFZqr-dqswQKJx-KjNHmB6eQTuTJoeLyuvMOLWAFgZfVWlXtK--bjvwuiVlkEFUHu5Mok6kuIswyfU6wHTMJLIrm6PNFVWvGIkWLIccb19IMvSa2A1ELrRmot4W8SDzC2JND6axDvXfhYW2w7KtV7qjCwgB-E0sILJhoh49-1oMaZqfZirTfKQr4_5OeidaFgSutTLfNPdeC-BiS-7OvAHOYB_3sAfrZMTSs9zmxCbKhAqFTm_pDDpc-rVzA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ZH_2OOzMKCaqOiqFBjD4qh7JvRO46B0AunUXnvVvsn5SW5Q6pKQ4bP-O18Biaosau1gWav3XD9QF0DjCWGosGRAoUulcjsJvuj5cGiEgFJR9nD_MH1v3Cziwq6nP1I-Bh2d3p22OQmH81AquMz8Dap3JAJSvavIg","expires_in":3600}
         : URL: https://halo.sondelaconsulting.com/api/tickets
         : HTTP Status: 400
         : Received Content: {"summary":["Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'HaloClassLibrary.Models.Faults[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'summary', line 1, position 11."]}
20:03:35 : Halo Desktop App Ended
``

I have a URL Connection called APISocket and a contentreceived Event Handler which has this code in

System.DebugLog("URL: " + url)
System.DebugLog("HTTP Status: " + Str(httpStatus))
System.DebugLog("Received Content: " + content)

If httpStatus = 200 Then
  //MessageBox("Received content: " + content)  ' Debugging line
  
  textArea1.Text =  ("Received content: " + content)
  
  
  
  If url = "https://halo.sondelaconsulting.com/auth/token?tenant=sondella" Then
    Var json As New JSONItem(content)
    AccessToken = json.Lookup("access_token", "")
    SendPOSTRequestAndCreateTicket()
  ElseIf url = "https://halo.sondelaconsulting.com/api/tickets" Then
    Var json As New JSONItem(content)
    Var ticketID As String = json.Lookup("id", "")
    If ticketID <> "" Then
      MessageBox("Ticket created successfully! Ticket ID: " + ticketID)
    Else
      MessageBox("Ticket not created. Check the API response.")
    End If
  End If
Else
  MessageBox("An error occurred: " + Str(httpStatus))
End If

On the submit button I call the following method
SendPOSTRequestAndGetAccessToken()
// Note that the ticket creation will proceed asynchronously once the token is received

Then in that method, I have the following code


// Create JSON payload
Var json As New JSONItem
json.Value("summary") = SummaryTextBox.Text
json.Value("details") = DescriptionTextArea.Text
json.Value("status_id") = 1
json.Value("client_id") = 1

// Set the Authorization header
APISocket.RequestHeader("Authorization") = "Bearer " + AccessToken

// Set the content and Content-Type
APISocket.SetRequestContent(json.ToString, "application/json")

// Send POST request to create a ticket
APISocket.Send("POST", "https://halo.sondelaconsulting.com/api/tickets")

Then in the method called

SendPOSTRequestAndGetAccessToken

I have the following code

Var authUrl As String = "https://halo.sondelaconsulting.com/auth/token?tenant=" + EncodeURLComponent("sondella")

Var data As String

data = "grant_type=client_credentials"

data = data + "&client_id=" + EncodeURLComponent(“ ID CODEGOESHERE)

data = data + "&client_secret=" + EncodeURLComponent(“SECRETGOES HERE ")

APISocket.SetRequestContent(data, "application/x-www-form-urlencoded")

APISocket.Send("POST", authUrl)

When I run the code I get that information above and a 400 error

Any help appreciated

HTTP 400 is “Bad Request” meaning your request is bad in some way. The response from the API suggests your JSONItem is constructed incorrectly. Check the documentation for the API and correct your JSONItem as the API expects.

A simple, cute reference for HTTP codes is https://http.cat

If you need help building Xojo apps that interact with remote REST APIs, it is one of my consulting specialties.

I think the answer to your question is actually contained in the response from the API, namely, that your JSON payload contains JSON objects (the equivalent of a Xojo Dictionary), and it needs to contain an array. The specs for the API itself probably explain this in more detail. But you’ll have to do something similar to this (i.e. you can’t simply copy/paste this code, you’ll have to make changes):

Var jsonOut() As Dictionary
Var jsonItem As Dictionary = New Dictionary
jsonItem.Value("summary") = SummaryTextBox.Text
jsonItem.Value("details") = DescriptionTextArea.Text
jsonItem.Value("status_id") = 1
jsonItem.Value("client_id") = 1
jsonOut.Add(jsonItem)

Var jsonPayload As String = GenerateJSON(jsonOut)

The above will create a JSON array (which the API seems to require), but to properly format the jsonItem you’ll have to read the API specs.