HTTPSecureSocket stopped working...

I’ve got this data mining app that uses a JSON API. It’s been working fine for months now.
All of a sudden it stopped working, now generating an exception error, “Key Not Found”

This is the code that’s been working…

Dim jsonData As String Dim socket1 As New HTTPSecureSocket jsonData = socket1.Get("https://pro-api..." // api key not included in example

Also, when I put the https URL into my browser with the full parameter list and my API key, and the data is retrieved and displayed fine, (in my browser).

During testing, I set up Listbox with test data and used that, so as not to run up the meter on my API key. When I load this test data from my listbox text, all is well. But then as soon as I try to use the socket1.get(“http://…”) function, I get this exception error. Then I tried pasting in the new data from my browser, into the listbox as test data, and that works fine too. So… I’ve narrowed this down to something about the HTTPSecureSocket function.

I haven’t touched this code in months. Any ideas where I might look next??

Currently using Windows 2019r1.1. Also tried 2019r1.

maybe they redirect things in a browser that you’re not accounting for using the http socket ?
so you get back nothing but the redirect and things go wonky from there
whats in jsonData immediately after the get and before you process it ?

The jsonData string, (when looked at in the debugger), is empty. Then I get an exception error on the next line of code.

Dim apiResult As New JSONItem(jsonData)

which suggests to me something has changed about the service and you need to update your code

  1. they added redirects that GET wont automatically follow but a browser will
  2. you need a specific browser user agent string
  3. you need to update what connection type you’re using (many had to do this)

[quote=443322:@paul townsend]I’ve got this data mining app that uses a JSON API. It’s been working fine for months now.
All of a sudden it stopped working, now generating an exception error, “Key Not Found”

This is the code that’s been working…

Dim jsonData As String Dim socket1 As New HTTPSecureSocket jsonData = socket1.Get("https://pro-api..." // api key not included in example

Also, when I put the https URL into my browser with the full parameter list and my API key, and the data is retrieved and displayed fine, (in my browser).

During testing, I set up Listbox with test data and used that, so as not to run up the meter on my API key. When I load this test data from my listbox text, all is well. But then as soon as I try to use the socket1.get(“http://…”) function, I get this exception error. Then I tried pasting in the new data from my browser, into the listbox as test data, and that works fine too. So… I’ve narrowed this down to something about the HTTPSecureSocket function.

I haven’t touched this code in months. Any ideas where I might look next??

Currently using Windows 2019r1.1. Also tried 2019r1.[/quote]
A KeyNotFoundException has nothing to do with your API key. This exception is happening because you are blindly requesting data from your JSONItem without checking if they are there already. Kinda like this:

// { "one" : 1, "two" : 2 } dim v as integer = js.value("three")

Since you’ve stated that the returned string is empty, that’s where your exception is coming from.

Now remember… HTTPSecureSocket is not the same as a browser. For instance, browsers send “User-Agent” headers and they’re almost all http/1.1 now (which the HTTPSecureSocket is not).

I suggest trying out the newer URLConnection object to see if it solves your issue.

Found the URLConnectionGet.xojo_binary_project in the Examples Projects folder.

Dim content As String = WebConnection.SendSync("GET", URLField.Text, 30) ContentArea.Text = content

That worked!

Also added a Try/Catch check. Thanks Norm. Thanks Greg.