Synchronous http get with headers

Hi all,
I want to make a synchronous https get call that retrieves some json from a webserver. I want to decode the json object and return it. it should look something like this:
 

function getFromServer()    local myJson = {}    doNetworkCall()    myJson = json.decode(networkCallResult)    return myJson end

 
I tried it with network.request but this only works with callbacks? thus I cannot return the object. My network call also has to except headers.
I also tried using socket.http but didn’t understand how it worked.
 
Thanks in advance!
Rick Slot

----edit:
I forgot to mention that I also need to use HTTPS. I tried it with socket.http, but it doesnt work with https. I saw that I could use luasec, but it seems a bit devious to install all kind of things with ‘make’. I really need to focus on simplicity.

Hi Rick.  I’m afraid you are going to have to figure out how to do this with network.request and use the call back.  The socket.http does not support HTTPS.  You should be able to get the full headers but its a far more complex process.

If you want to simulate a syncronous call, you can show a native.setActivityIndicator (http://docs.coronalabs.com/api/library/native/setActivityIndicator.html), call network.request() and move the code that processes the JSON data inside the call back (or put it in a function and call that function from the call back).

Rob

Hi Rob,
Thanks for your reply. I think I’ll be just using network.request then. The problem is that I’m writing some kind of library that connects to our rest api and I don’t want the user to be saddled with all kind of networking stuff. But I guess that theres no other way than letting them write some functional code then.

Rick

You can make them pass you a function that you call back to when done giving them the data you want. 

Rob

Hi Rick.  I’m afraid you are going to have to figure out how to do this with network.request and use the call back.  The socket.http does not support HTTPS.  You should be able to get the full headers but its a far more complex process.

If you want to simulate a syncronous call, you can show a native.setActivityIndicator (http://docs.coronalabs.com/api/library/native/setActivityIndicator.html), call network.request() and move the code that processes the JSON data inside the call back (or put it in a function and call that function from the call back).

Rob

Hi Rob,
Thanks for your reply. I think I’ll be just using network.request then. The problem is that I’m writing some kind of library that connects to our rest api and I don’t want the user to be saddled with all kind of networking stuff. But I guess that theres no other way than letting them write some functional code then.

Rick

You can make them pass you a function that you call back to when done giving them the data you want. 

Rob