Load JSON from the Web

  1. 6 months ago

    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:

    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

    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?

    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.

  2. Greg O

    20 Apr 2017 Xojo Inc Somewhere near Raleigh, NC

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

  3. Tomas J

    20 Apr 2017 Pre-Release Testers, Xojo Pro Europe (Germany)
    Edited 6 months ago by Tomas J

    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)
  4. @Greg OLone Have you stepped through with the debugger to see where it's actually failing?

    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.

  5. @Tomas J dim Credentials as string = Base64Encode("username and/or password and/or hash")

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

  6. Emile S

    20 Apr 2017 Europe (France, Strasbourg)
    Edited 6 months ago by Emile S

    From where did you get Base64Encode ?

    Edit:

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

  7. Tomas J

    20 Apr 2017 Pre-Release Testers, Xojo Pro Europe (Germany)

    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

  8. Greg O

    20 Apr 2017 Xojo Inc Somewhere near Raleigh, NC

    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.

  9. @Greg OLone sock variable in the debugger

    I'm sorry, but I'm not sure what you really want to say...

    Is it something like this:

    [b]Name[/b]		[b]Value[/b]
    
    				Globals
    d				Dictionary
    me				WebButton
    result
    self			        (name of the project)
    Socket			        HTTPSocket

    Or something like this:

    App
    Runtime
    Threads⌄
    Serials⌄
    Modules⌄
  10. Richard G

    20 Apr 2017 Pre-Release Testers, Xojo Pro Atlanta, GA

    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 :)

  11. Greg O

    20 Apr 2017 Xojo Inc Somewhere near Raleigh, NC

    @Ricardo G I'm sorry, but I'm not sure what you really want to say...

    Is it something like this:

    [b]Name[/b]		[b]Value[/b]
    
    				Globals
    d				Dictionary
    me				WebButton
    result
    self			        (name of the project)
    Socket			        HTTPSocket

    Or something like this:

    App Runtime Threads⌄ Serials⌄ Modules⌄

    Sorry, I meant Socket. If you click on the word Socket, you can inspect the properties inside it.

  12. @Greg OLone Socket HTTPSocket

    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.

  13. Greg O

    21 Apr 2017 Xojo Inc Answer Somewhere near Raleigh, NC

    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.

  14. Tomas J

    21 Apr 2017 Pre-Release Testers, Xojo Pro Europe (Germany)

    as said before... HTTP Basic Auth...

  15. @Greg OLone 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.

    I did it and it works. Thank you very much ;)

or Sign Up to reply!