Skip to content

Instantly share code, notes, and snippets.

@wtfbbqhax
Created November 3, 2016 15:25
Show Gist options
  • Save wtfbbqhax/3b5d3410cf70b45e0315c4b63697d9d0 to your computer and use it in GitHub Desktop.
Save wtfbbqhax/3b5d3410cf70b45e0315c4b63697d9d0 to your computer and use it in GitHub Desktop.
A sample Restful JSON Client API
--[[
_____ _ _
|_ _| __ ___ _ __ ___ _ _| | ___ _ _ ___ | | _ _ __ _
| || '__/ _ \ '_ ` _ \| | | | |/ _ \| | | / __|_____| | | | | |/ _` |
| || | | __/ | | | | | |_| | | (_) | |_| \__ \_____| |__| |_| | (_| |
|_||_| \___|_| |_| |_|\__,_|_|\___/ \__,_|___/ |_____\__,_|\__,_|
Victor Roemer [WTFBBQHAX]
Nov. 03, 2016 10:53:27AM EST
A sample Restful JSON Client API.
This sample demonstrates how to comunicate with an HTTP JSON 3rdparty
API in Lua script.
APIs demonstrated:
* HTTP RestClient
* JSON
--]]
-- GitHub API URL
url='https://api.github.com/repos/wtfbbqhax/tremulous/releases'
-- HTTP Get request- retrieve the raw JSON response
txt = http.get(url)
assert(txt.code == 200)
-- Decode raw JSON response into a Lua table
releases = rapidjson.decode(txt.body)
-- GitHub returned an array of releases- The "most_recent" is item 1
-- NOTE: Lua array indexing starts at `1` not `0`!!
most_recent = releases[1]
-- FIXME: Remove hardcoded tag_name in this test
assert(most_recent.tag_name == "Oct-22-2016")
for i,asset in ipairs(most_recent.assets) do
print(asset.browser_download_url)
end
@wtfbbqhax
Copy link
Author

wtfbbqhax commented Nov 3, 2016

Screenshot

screen shot 2016-11-03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment