How to pass data with network.request?

I’m trying to use network.request for my app. The example app works fine. However, when I modify it to pass the data to a variable, instead of just printing the data, the value is set to nil.

Original example from anscamobile.com (Works ok, but only prints the data without passing it along):

local function networkListener( event )  
 if ( event.isError ) then  
 print( "Network error!")  
 else  
 print ( "RESPONSE: " .. event.response )  
 end  
end  
   
-- Access Google over SSL:  
network.request( "https://encrypted.google.com", "GET", networkListener )  

what I’m trying to do (notice the new variable ‘response’, to which i’m trying to pass the data to):

local response;  
local function networkListener( event )  
 if ( event.isError ) then  
 print( "Network error!")  
 else  
 response = "RESPONSE: " .. event.response  
 end  
end  
   
-- Access Google over SSL:  
network.request( "https://encrypted.google.com", "GET", networkListener )  
  
print (response);  

(‘response’ returns NIL instead of the data.) [import]uid: 33608 topic_id: 16243 reply_id: 316243[/import]

response needs to be inside of the networkListener function [import]uid: 24641 topic_id: 16243 reply_id: 60486[/import]

So how can I pass the fetched data to a variable outside the function?
Thanks [import]uid: 33608 topic_id: 16243 reply_id: 60488[/import]

make it a global using _G [import]uid: 24641 topic_id: 16243 reply_id: 60492[/import]

you can also try the socket.http method

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16243 reply_id: 60515[/import]

Thanks,
I finally figured out how to use LUa Socket for the job. Basically something like:

local function fetchData(u)  
 local http = require("socket.http");  
 local ltn12 = require("ltn12");  
 local t={};  
 local respt = http.request{url = u , sink = ltn12.sink.table(t) };  
 http = nil; --not sure if these two lines are really needed, but cleaning the vars just in case  
 ltn12 = nil;  
 return table.concat(t);  
end  
  
local myDataStream = fetchData("http://www.google.com")  

I still can’t understand how to get the data through network.request. It seems like the value in event.response cannot be passed anywhere. [import]uid: 33608 topic_id: 16243 reply_id: 60547[/import]

It can be… you have to think differently, you are thinking in the waterflow or serial flow of events…

here’s one way to look at things

try to have a read of this article and you might get one way of doing things.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16243 reply_id: 60548[/import]

Could anyone from the Ansca staff please take a look at this?

P.S.
Thanks JayantV for the link and for the help (in this post and others!). unfortunately I couldn’t figure out a solution based on the article you provided, so I’m still using the walk-around solution. Wonder if someone from Ansca could clarify how to solve this. [import]uid: 33608 topic_id: 16243 reply_id: 61774[/import]

I think your print line is being executed way before the network request has had a chance to respond. Your network listener is only called in the “event” of a response whereas your print statement happens immediately.

You could try making a separate function that prints the response variable and call that function from inside the network listener function after the response has been recorded. [import]uid: 9422 topic_id: 16243 reply_id: 61777[/import]

Thanks, this makes sense. I guess I’ll have to rework the structure of the app so that the function triggers a “chain reaction” to process the received data. [import]uid: 33608 topic_id: 16243 reply_id: 62000[/import]