Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Bitstrips

Need urgent advice on working with API data through internet.request()

Question

It seems completely funny but I am actually trying to get through this for the last 2 days:

I try to access weather data through the OpenWeatherMap.org API wich seems to work fine with internet.request(url).

The problem is just that it returns me the JSON as a table instead of a String. I tried to access fields of that table but everything seems to return nil. I just can't get to access anything from that table. There are tons of JSON serialization librarys out there but they all need the JSON as a String. I am completely lost rn.

This is the API response that gets stored as a table for some reason:

{"coord":{"lon":7.68,"lat":52.85},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"stations","main":{"temp":10.51,"pressure":999,"humidity":66,"temp_min":10,"temp_max":11.11},"visibility":10000,"wind":{"speed":5.1,"deg":230},"rain":{"3h":0.11},"clouds":{"all":92},"dt":1552771115,"sys":{"type":1,"id":1871,"message":0.0045,"country":"DE","sunrise":1552714945,"sunset":1552757625},"id":2810860,"name":"Werlte","cod":200}

 

My Lua code couldn't be simpler:

local web = require("internet")
local data = web.request(<API_LINK>)

-- data is of type table and I don't know how to get any values. Tried so many combinations on indices.
-- For each loop works to loop through everything though

 

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Pretty sure you need to use the HTTP Request Object API that component.internet.request returns. It is a userdata table with methods. I broke my MC launcher so I cant test this but you can try something along these lines..

Got a chance to test and the below code I have didn't quite work... I've updated it

local success, response = pcall(web.request, "<your api call>")
if not success then error("Something sucks here..") end

local content = ""
for chunk in response.read do -- # changed to response.read
  content = content .. chunk
end

doStuffWithData(content) -- # yay!?

 

Edited by Molinko
response.read seems to work
Link to post
Share on other sites
  • 0
17 hours ago, Molinko said:

Pretty sure you need to use the HTTP Request Object API that component.internet.request returns. It is a userdata table with methods. I broke my MC launcher so I cant test this but you can try something along these lines..

Got a chance to test and the below code I have didn't quite work... I've updated it


local success, response = pcall(web.request, "<your api call>")
if not success then error("Something sucks here..") end

local content = ""
for chunk in response.read do -- # changed to response.read
  content = content .. chunk
end

doStuffWithData(content) -- # yay!?

 

 

16 hours ago, CptMercury said:

Yeah, @Molinko‘s code should work, response (in your code it’s „data“) is an iterator suited for „for .. in“ loops. You don‘t actally have to pcall it, calling web.request works also fine.

 

 

So basicly I fixed it in a different matter:

While iterating through data with ipair I managed to see that data contained two functions for some reason appliable on data. When I wrote the variable as a function => data() It showed me the correct JSON String and I could use it with the json lua library to create a usable table. Thanks for your responses tho

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.