Post Rest String Format and more

I’m desperately trying to figure out how to send a Rest Post. I’d like to know if this string is valid

Dim json As String =“{”“firstName”“:”“ROSSI”“,”“lastName”“:”“TIZIANA”“,”“birthDate”“:”“06-06-1989"”,““Sex””:F}"

On Thom McGrath’s advice, I tried following the instructions in the documentation, but beyond this code, I can’t proceed.

Var Conn As New URLConnection
dim response as String
Conn.RequestHeader(“Content-Type”) = “application/json”
Dim json As String =“{”“firstName”“:”“ROSSI”“,”“lastName”“:”“TIZIANA”“,”“birthDate”“:”“06-06-1989"”,““Sex””:F}"
Conn.SetRequestContent(json, “application/x-www-form-urlencoded”)
response= ???
Conn.Send(“POST”, “https://xxxx./xx/xxx”)

How do I get the response from the server?
How do I enter username and password?
Can anyone help me?

Conn.SetRequestContent(json, "application/x-www-form-urlencoded")

Should probably be:

Conn.SetRequestContent(json, "application/json")

Try:

Dim json As String ="{""firstName"":""ROSSI"",""lastName"":""TIZIANA"",""birthDate"":""06-06-1989"",""Sex"":F}"
Var Conn As New URLConnection
Conn.SetRequestContent(json, "application/json")
dim response as String = Conn.SendSync("POST", "https://xxxx./xx/xxx")

As an aside, I recommend using JSONItem rather than constructing your own JSON string. It will be easier to read and maintain.

2 Likes

There are a number of ways a username/password might be sent here. The documentation for the REST api should explain what they expect.

Sorry, I meant to reply in the other thread but got distracted and then forgot. I was working under the assumption that you had experience with the HTTPSocket code you had posted. URLConnection and HTTPSocket work similarly.

URLConnection, like HTTPSocket, is event driven. You’ll either need to place it on a window, create a subclass, or for advanced uses, use AddHandler to hook up methods to its events. Don’t use SendSync unless you’re using the URLConnection on a thread, as you need to assume the user’s internet is unreliable. Using SendSync on the main thread will lock your application and give you the spinning beachball on Mac or the “not responding” message on Windows if the request lasts more than 2 seconds.

Assuming HTTP basic authentication, you generate the authentication header exactly as you originally posted: encode the username and password in base64. You already have this code, but I’ll include it below. However, only the API provider can tell you to authenticate. As a side note, HTTP basic authentication is only acceptable under TLS/SSL, but any API provider allowing it as an option should know this and wouldn’t allow an insecure connection.

Since you’re sending a JSON body, use JSONItem to create your payload and set the request content type to application/json.

Look for the response in the ContentReceived event, or an error in the Error event. But beware, not every error is considered an error to the socket. A successful request that fails to authenticate or file the desired endpoint is still considered a success request. The Error event is most often only fired for network/connection errors. Still, you should be prepared to handle the possibility.

This code assumes you’ve dropped a URLConnection1 on the window and is triggered through a button push.

Var Body As New JSONItem
Body.Value("firstName") = "ROSSI"
Body.Value("lastName") = "TIZIANA"
Body.Value("birthDate") = "06-06-1989"
Body.Value("Sex") = "F"

URLConnection1.SetRequestContent(Body.ToString, "application/json")
URLConnection1.RequestHeader("Authorization") = "Basic " + EncodeBase64(Username + ":" + Password, 0)
URLConnection1.Send("POST", "https://endpoint")

Then wait for the ContentReceived event.

I’m also really bothered by that ambiguous date format, but that’s a separate topic entirely.

3 Likes

You guys were great, thank you all. I have one last question:
To form the string, Thom suggested I use Body.Value. It’s very simple to use, but if there are multiple elements, in my example multiple people, what should I add between each person?

You would create multiple JSONItems.

Var RecordOne As New JSONItem
RecordOne.Value("key") = "value"

Var RecordTwo As New JSONItem
RecordTwo.Value("key") = "value")

Var RecordList As New JSONItem
RecordList.Add(RecordOne)
RecordList.Add(RecordTwo)

The resulting JSON would look something like this, though compacted.

[
  {
    "key": "value"
  },
  {
    "key": "value"
  }
]
2 Likes