I need your help preparing to send invoices (headers and lines) via a REST API.
I need to construct a request body containing an array of headers (even if it consists of just a single element); each header is made up of a set of properties and an array of lines.
Since I don’t have much experience with JSON, I’m struggling to put this together.
What have you tried? What issue(s) are you seeing? What’s the REST API look like (documentation)? Have you taken a look at the REST API example included with Xojo?
have a look at class URLConnection (there are fragments for send JSON too)
the http header becomes Content-Type: application/json; charset=utf-8
and the count of bytes of the body Content-Length: 1234
the end of a raw header are 2x CRLF
followed by body data.
the json object is send as .ToString in the request body
It works a lot like Dictionary, but can act like an array too.
I might do something like:
Var Headers As New JSONItem("[]")
Headers.Add("value 1")
Headers.Add("value 2")
Headers.Add("value 3")
Var Lines As New JSONItem("[]")
Lines.Add("value 1")
Lines.Add("value 2")
Lines.Add("value 3")
Var Document As New JSONItem("{}")
Document.Value("Document_Header") = Headers
Document.Value("Document_Line") = Line
Two things to note here:
This is probably not correct for your implementation, but I’m working with the information I have.
Passing "[]" and "{}" is absolutely not necessary. I like to do it as a hint to my future self that the item should be an array vs a structure. It also prevents you from accidentally switching the JSONItem to the other mode before any content is added.
I’ve given you the basic premise. You’re going to need to create a JSONItem struct with the keys key_documento, prg_riga, and ind_tiporiga. You then add that to a JSONItem array, and add that array as the value for a DocumentiR key in a different struct. And so on. There’s definitely some nesting to do.
If you mean you don’t know how to get the JSON string to send over the socket, a quick look at the documentation would point you to the ToString method. Its default parameters will serve just fine for a REST API.