Network.Request: event.response not being saved into local variable

Hello,

I am trying to write a program that pulls a webpage and extracts certain text from it. I am using a pattern to extract the text but I cannot figure out how to save the extracted text into a table. Any idea what I am doing wrong? When I debug it says my local variable is nil even after I said it to the event.reponse()

Details: I am using Sublime on a Windows machine

I also attached a screenshot of my current event.response() output

[lua]

local restaurants = {}

local yelpString = “”

–this method tells the program what to do once the website is retrieved
local function networkListener( event )

   if ( event.isError ) then
      print( "Network error: ", event.response )
   else
      yelpString = event.response

      --loops through the website to find the pattern that extracts restaurant names and prints it out
      for i in string.gmatch(yelpString, “(.-)<”) do
         table.insert(restaurants, i)
         print(i)
      end
   end
end

– retrieves the website
network.request( “https://www.yelp.com/search?cflt=restaurants&find_loc=Cleveland%2C+OH%2C+US”, “GET”, networkListener )

[/lua]

You should look at their REST based API.  See: https://www.yelp.com/developers/documentation/v2/search_api

This will allow Yelp to return JSON data to you, then you can use our json.decode() API to unpack event.response and return a Lua table for you to use.

Rob

Hello,

Thanks for introducing me to the Yelp API!

But I am still having the same problem. I unpacked the JSON file into a Lua table but when I try to access the information in the table outside of NetworkListener the table returns nil.

How do I fix this? 

I want to get the names of the restaurants it returns and assign them random numbers. Does this have to be done inside of NetworkListener? I want to create a new function to do it but I can’t do anything with a nil table

local restaurants = {} local yelpString = "" local json = require( "json" ) local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) else yelpString = event.response restaurants = json.decode(yelpString) print ( "RESPONSE: " .. json.prettify(yelpString)) end end network.request( "https://api.yelp.com/oauth2/token?client\_id=myClientID&client\_secret=myClientSecret", "POST", networkListener) local headers = {} headers["Authorization"] = "Bearer my\_Access\_Token" local params = {} params.headers = headers network.request( "https://api.yelp.com/v3/businesses/search?term=delis&latitude=41.4993&longitude=-81.681290", "GET", networkListener, params)

Normally, you would have to wait until you got a valid response to the authorisation call before making any follow on calls.  Your code will need to be rewritten to synchronously call network.request() based on valid responses.  (At the moment your code is asynchronous)

You should look at their REST based API.  See: https://www.yelp.com/developers/documentation/v2/search_api

This will allow Yelp to return JSON data to you, then you can use our json.decode() API to unpack event.response and return a Lua table for you to use.

Rob

Hello,

Thanks for introducing me to the Yelp API!

But I am still having the same problem. I unpacked the JSON file into a Lua table but when I try to access the information in the table outside of NetworkListener the table returns nil.

How do I fix this? 

I want to get the names of the restaurants it returns and assign them random numbers. Does this have to be done inside of NetworkListener? I want to create a new function to do it but I can’t do anything with a nil table

local restaurants = {} local yelpString = "" local json = require( "json" ) local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) else yelpString = event.response restaurants = json.decode(yelpString) print ( "RESPONSE: " .. json.prettify(yelpString)) end end network.request( "https://api.yelp.com/oauth2/token?client\_id=myClientID&client\_secret=myClientSecret", "POST", networkListener) local headers = {} headers["Authorization"] = "Bearer my\_Access\_Token" local params = {} params.headers = headers network.request( "https://api.yelp.com/v3/businesses/search?term=delis&latitude=41.4993&longitude=-81.681290", "GET", networkListener, params)

Normally, you would have to wait until you got a valid response to the authorisation call before making any follow on calls.  Your code will need to be rewritten to synchronously call network.request() based on valid responses.  (At the moment your code is asynchronous)