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.