Using Sockets to make posts

I have been trying to use a HTTPSecureSocket to make a URL post to GPT3 using the openai url
The reason I’m doing this is because URLConnection.send has a bug where the HandleURL session crashes when the ContentReceived event has content greater than 200 bytes. So I decided to experiment with sockets directly.

Unfortunately it’s not working, or at least I can’t tell.

Here is my code:

var x as string
var sendString as string = gptquery
sendString = sendString.ReplaceLineEndings(“\n”)

s = “{” + EndOfLine
s = s + q + “model” + q + “:” +q + “text-davinci-003” + q + “,” + EndofLine
s = s + q + “prompt” + q + “:” +q + sendString+ q + “,” + EndofLine
s = s + q + “temperature” + q + “:” + “0” + “,” + EndofLine
s = s + q + “max_tokens” + q + “:” + “2000” + EndofLine
s = s + “}”

var bearer as string = “sk-98398n98ndisnfiubva8” //key string

Dim socket1 As New ASocket
socket1.SSLConnectionType = SSLSocket.SSLConnectionTypes.TLSv1
socket1.SetRequestHeader(“Accept-Encoding”, “application/json; charset=utf-8”)
socket1.SetRequestHeader(“Authorization”, bearer)
socket1.SetRequestContent(s, “application/json”)


Dim start As Double = Microseconds
Dim endTime As Double = start + (1000000 * 300) // 1 second in microseconds
socket1.returnString = “–”
while(socket1.completeURL = false AND Microseconds < endTime)

//dim result as string = parseGPTResults(jsonData)
return socket1.returnString

I created a Socket Class with a parent of HTTPSecureSocket

I added a PageReceived event which includes:

me.returnString = content
if content.IndexOf(“doctype”)=-1 then
if content.IndexOf(Chr(34) + “error” + Chr(34) + “:”)=-1 then
var json as New JSONItem(content)
var j as JSONItem = json.Value(“choices”)
var result0 as JSONItem = j.ValueAt(0)
var s As String = result0.Value(“text”)

  me.returnString = s
end if

end if

catch e as JSONException
me.completeURL = true
me.returnString = e.Message
end try

me.completeURL = true

But the PageReceived event is never called.
The Error event is called with a code of 102

Does anyone have any ideas?

Not sure if this is related to your problem, but I believe this should be:
socket1.SetRequestHeader("Authorization", "Bearer " + bearer)

i.e., the word “Bearer” a space and then your API key.

I’m also not convinced the HTTPSecureSocket can talk to OpenAI’s API successfully. I suspect that either OpenAI is using HTTP1.1 features, or requires a version of TLS, that the HTTPSecureSocket doesn’t support.

I’ve got OpenAI’s API working well in Xojo desktop apps. Haven’t tried it in a web app, but I don’t see why it shouldn’t work. It uses the URLConnection or curl, but not the HTTPSecureSocket. Take a look:


Thank you Andrew for your response. I have already tried the "Bearer " idea with no luck. So I must be doing something else incorrect.

As I mentioned earlier in the post, I have URLConnection working to make the posts, but it is crashing the HandleURL thread when the reply has content that is large.

What I am looking for is some guidance regarding doing posts using HTTPSecureSocket. Again, my hope is that this method will not crash the HandleURL thread.

If someone has a working version showing how to make HTTPSecureSocket work for URL posts specifically for OpenAI that would be helpful.

Hi Andrew,

Your GitHub site looks great. Very impressive work.

I looked into your code and you are using URLConnection.


I don’t think the HTTPSecureSocket can work with the OpenAI API. I tried to get it working back in January, but concluded that it wasn’t going to work. The HTTPSecureSocket is just too old.

I have been using Xojo and URLConnection to access the OpenAI API for a couple of weeks with no dramas.

APIConnector is a UrlConnection Class with the events on it.

I am using 2022 R4.1

Dim data As New Dictionary
data.Value("model") = GPTmodel
data.Value("max_tokens") = max_tokens
data.Value("temperature") = temperature
data.Value("prompt") = prompt

APIConnector.RequestHeader("Content-Type")= "application/json"
APIConnector.RequestHeader("Authorization")= "Bearer " + api_key
Dim j As New JSONItem(data)

APIConnector.SetRequestContent(j.ToString, "application/json")
APIConnector.Send("POST", endpoint_url,timeout)

Thanks Daniel,

Nice code. Have you tried running this in a HandleURL thread?
This is the problem I’m facing. I have no trouble on a webpage, but in HandleURL it crashes.

What version of Xojo are you using? Have you submitted this as an Issue?

I understand now. Your problem is running the code on the main thread from HandleURL, and you should not be doing any real processing there. I am guessing you are trying to run your own API to pass to openAI ?

Make sure you are using URLConnection asynchronously. In cases like these where a response is expected to take some time, it’s common to immediately respond with an http status code of 202 (Accepted) and then the client continues to send GET requests to see if there was a response.

1 Like

Yes. that is what I’m doing. I’m trying to implement a service. There is no other mechanism in Xojo for implementing a service.

The latest release 2022 v4.1
I submitted as an issue and it’s being reviewed.

I’m not sure how that works. URLConnection does not have provisions for repeated replies. There is only a ContentReceived which executes once as far as I know. It’s possible that it executes more than once, but I haven’t seen that and that is not problem. The problem I am experiencing is that as soon as it does reply, it crashes the HandleURL thread.

I’m not talking about URLConnection. The HandleUrl event in your Web app is where you would be implementing this. Basically, you accept the request, have it run in a thread and then it’s up to the client to make a new request every once in a while to see if it’s done, and when it is, it gets returned to the client.

1 Like

That would work for the client calling me, but doesn’t solve the problem of me calling another service. The service I’m calling is causing HandleURL to crash due to the size of the response.

And Greg is telling you to make your large request to this service not happen in HandleURL.

FWIW, the process he describes is exactly how my Issues Linker app works.

This is an interesting subject calling for a Xojo technical blog post like “Solving HandleURL data exchange limitations” or something like that.

1 Like

So let me clarify…

You’re calling into a web app which is then calling out to another service? Then yes, you definitely need to be using an async URLConnection in that case. I’d even go so far to say that you might want to use a helper app that you call using a shell (curl would do) so you don’t lock up the main app.

You’ve said “causing HandleUrl to crash” a few times. Are you saying that the whole app crashes while the HandleURL event is running? Or that an exception is happening that you’re not catching?

1 Like

I’m happy to share the information I’ve gathered over the past 8 years with anyone who helps put food on my table.

Blog posts about “everyday Xojo” are a Xojo duty. If they would like to hire you to write some posts, I certainly will applaud both, but if they write them by themselves as usual, it’s ok too.

You should try to publish some content on Udemy about your expertise in the many areas you domain, that would create some recurrent cash, as Xojo Consulting is very seasonal as Bob Keeney said once.