Howdy, Stranger!

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

Anyone have http.request working inside a class?

edited September 2012 in Questions Posts: 666

When I use http.request as a global function, things work well. However, if I make it a part of a class, it loses the data being sent to the callback.

For example:


Loader = class() function Loader:init() self.files = {} end function Loader:AddFile(file) --, destobj) table.insert(self.files, {file}) end function Loader:Load() for _,master in pairs(self.files) do for k,v in pairs(master) do http.request( v, self.Process ) -- calls, but fails end end end function Loader:Process( data, status, headers ) print( "Response Status: " .. status ) -- data holds my 200! -- Store in our global variable print (#data .. " bytes long") -- Check if the status is OK (200) if status == 200 then -- do work else print( "Error downloading file" ) end end

When this is run, the field for data in the callback function actually holds the value for status (200) when the http request comes back.

Is there something I'm not calling or setting up correctly?

To work around this, I wrote a single thread lazy loader. but I can see from some experiments I did that http.request doesn't seem single thread bound.

Tagged:

Comments

  • NatNat
    Posts: 143

    When you refer to self.Process in the statement "http.request( v, self.Process )", the Process method is not bound to self (as is the case in Python, for example).

    Instead, you need to do:

    http.request(v, function(data, status, headers)
        self.Process(data, status, headers)
    end
    
  • edited September 2012 Posts: 666

    Tanks, Nat. Makes sense. I ended up needing to use self:Process, though.

  • Posts: 2

    I am having a similar issue. I have the need to create a class where the object would need its own instance of http.requests and parsing. I can get everything fine in main, but when i try to put it in a class, it always gives the error that the #2 argument expected to be a function... which it is, but its not handling it correctly for some reason. I've tried self:function as well as function(data, status, headers) and also self:function(data, status, headers) and nothing seems to work.

  • edited August 2 Posts: 108

    @hotwire I realize super old thread but for the sake of it I posted what I did:
    I needed to change the function to self.function rather than self:function, with no () or anything.

    http.request( "https://www.shadertoy.com/api/v1/shaders/MdX3Rr?key=N......m", ShaderToy.decoder )
    
  • dave1707dave1707 Mod
    Posts: 6,397

    @AxiomCrux I don't think you're going to get an answer from hotwire. He hasn't been on the site since he wrote the above post 4 years ago. Also, did you mean to show your key in the above post.

  • Posts: 108

    Oh man, I totally forgot about this, ok now I remember. thank you so much for removing that

  • Posts: 108

    for that matter, my reply ended up being revised when I had to do it from inside a class function.. derp. Again thank you for noticing that and redacting it :) I had never seen a moderator edit a forum post before, didn't realize you could do that, must feel powerful :P` hahah

  • dave1707dave1707 Mod
    Posts: 6,397

    @AxiomCrux Being a moderator gives me a lot of options a normal user doesn't have. I do a lot of banning of spammers and deleting discussions that don't pertain to Codea in order to keep this forum clean.

Sign In or Register to comment.