Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

HTTP Sync - Synchronous HTTP Requests - http.requestSync()

Hi all,

Working with WebRepo I became fed up with managing multiple nested callbacks for http.request() calls so I now present ‘HTTP Sync’!

This library provides an implementation of http.requestSync() entirely avoiding the need for user defined callbacks.

Short example usage here:

-- Make our http request
local ok, data, status, headers = http.requestSync("")

-- Check that the http request succeeded
if ok then
    print("Something went wrong!\n" .. data)

Please be aware that this may not play nicely with the supportedOrientations library at the moment.

Hopefully this will be useful to someone.


Note: This is also available to download on WebRepo.

Tagged: 26.2K


  • edited June 2021 Posts: 1,302

    well, that's interesting. i can't say that i understand it after ten minutes of trying. why is all that mapping and faking needed? i'm sure missing something.

  • Posts: 327
    @RonJeffries I'll admit the Global Overrides stuff is pretty confusing but in essence it allows the HTTP Sync library to intercept the Codea runtime's setup() and draw() calls to make them run inside a coroutine that we can then suspend while waiting for the http request to complete.
  • Posts: 1,302

    thanks ... i'll have to think about, esp why we'd need to suspend draw. setup, i kind of get. intuitively, i'm have thought it could be simpler.

  • Posts: 327

    Without a doubt, in most cases it makes more sense to use the usual callbacks to avoid blocking execution but in some web API cases it makes life much easier to just wait for the response (Github OAuth Authentication being one).

    As for it being simpler, I don’t think so unfortunately. As the callbacks are called directly by the Codea runtime outside of calls to setup() and draw() it needs to be able to return execution to the runtime without continuing past the http.requestSync() call. coroutines are the only way I know of that makes this possible..

  • edited June 2021 Posts: 1,302

    yes i agree coroutines are the way. not sure it takes quite that much mechanism, but i sure haven't done it, so what do i know? it's cool, for sure.

Sign In or Register to comment.