Load JSON from the Web

Hi all,

I’m new to XOJO and I’m making a Web App to list the time entries of my co-workers

The Code looks like this:

[code]Dim Socket as new HTTPSocket
Dim d As New Dictionary
Dim result As String

Socket.SetRequestContent("",“application/json; charset=utf-8”)
result = Socket.get(“http://teamwork.companyname.com/time/total.json?userId=111111”, 30)
result = DefineEncoding(result, Encodings.UTF8)

OutputArea3.Text = result[/code]

When I replace the URL with www.example.com, it works and the content can be loaded.
I also tried it with various URL’s for different JSON’s from the Web, but it didn’t work too.

What should I use, so that JSON’s can be loaded?

Have you stepped through with the debugger to see where it’s actually failing?

try this one, I am using this to communicate via https with Nextcloud API (Here: the News App). Strongly suggest to use Paw if you deal with JSONs. Normally UserIDs etc shouldn’t be send in the URL. The URL.Text from below is something like: https://host/index.php/apps/news/api/v1-2

dim socket1 As new HTTPSecureSocket
dim Credentials as string = Base64Encode("username and/or password and/or hash")
dim data As String
  
socket1.Secure = True
  
// Haeder with Credentials
socket1.SetRequestHeader("Accept", "application/json")
socket1.SetRequestHeader("Content-Type", "application/json; charset=utf-8")
socket1.SetRequestHeader("Authorization", "Basic " + Credentials)
  
data = socket1.SendRequest("GET",  TheURL.Text + "/feeds", 120)

Thank you for the response, Greg.
Yes, I made a breakpoint at result = DefineEncoding(result, Encodings.UTF8) and found out that the variable result is empty.

Thanks for the code, but Base64Encode does not work: This Item does not exist.
What am I doing wrong?

From where did you get Base64Encode ?

Edit:

Maybe:
http://documentation.xojo.com/index.php/EncodeBase64
http://documentation.xojo.com/index.php/DecodeBase64

HTTP Basic Auth as documented here:
https://tools.ietf.org/html/rfc7617

As said I used it for the Nextcloud News App as documented here:
https://github.com/nextcloud/news/blob/master/docs/externalapi/External-Api.md

Credentials shouldn’t be send over in URLs because they are stored everywhere even if send over https. Something like “http://teamwork.companyname.com/time/total.json?userId=111111” is evil! Maybe a reason why your script doesn’t work in the net with other JSON APIs?

For more background:
https://media.ccc.de/v/33c3-8034-build_your_own_nsa

Tomas, lets focus on why his script isn’t working instead of the security implications of his implementation. You’re only confusing matters with all this extra info.

Ricardo, when you hit that’s breakpoint, click on the sock variable in the debugger and see what the httpstatuscode value is.

I’m sorry, but I’m not sure what you really want to say…

Is it something like this:

[code]Name Value

			Globals

d Dictionary
me WebButton
result
self (name of the project)
Socket HTTPSocket[/code]

Or something like this:

App Runtime Threads? Serials? Modules?

To help debugging as previously mentioned I make heavy use of PAW so I can see it all work outside of the Xojo environment. I’ve also used PostMan, a Chrome plugin too (freebie). Once I know my RESTful parameters all work I then plugin to Xojo. Cures most ills :slight_smile:

[quote=326960:@Ricardo Grebien]I’m sorry, but I’m not sure what you really want to say…

Is it something like this:

[code]Name Value

			Globals

d Dictionary
me WebButton
result
self (name of the project)
Socket HTTPSocket[/code]

Or something like this:

App Runtime Threads? Serials? Modules?[/quote]
Sorry, I meant Socket. If you click on the word Socket, you can inspect the properties inside it.

The _httpStatusCode is 401 (teamwork.companyname.com)
But when I replace the URL with http://httpbin.org/get it works.
I also tested it with https://api.guildwars2.com/, but it didn’t work.

401 means Not Authorized. You should look and see if that URL requires a username and password from a browser. If so, you’ll need to subclass the socket and implement the AuthorizationRequired event.

as said before… HTTP Basic Auth…

[quote=327089:@Greg O’Lone]
401 means Not Authorized. You should look and see if that URL requires a username and password from a browser. If so, you’ll need to subclass the socket and implement the AuthorizationRequired event.[/quote]

I did it and it works. Thank you very much :wink: