User Tools

Site Tools



minionlib

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
minionlib [2020/12/13 15:44] fxfireminionlib [2021/01/17 20:19] – Response headers string to table example kali
Line 250: Line 250:
 <code lua> <code lua>
 function SendHttpRequest() function SendHttpRequest()
- local API_Key = "XXXXXX-587C-3XXX-90B5-XXXXXXXXXXXXXXX3F-XXX-4A79-XXX-XXXXXXXXXX" +   local function success(str, header, statuscode) 
- local function success(str,header,statuscode)  +      d("HTTP Request: success."
- d("HTTP Request: success."+      d("HTTP Result Header: " .. tostring(header)
- d("HTTP Result Header: "..header) +      d("HTTP Result StatusCode: " .. tostring(statuscode))
- d("HTTP Result StatusCode: "..tostring(statuscode)) +
-  +
- local data = json.decode(str) +
- if data then +
- d("HTTP Request: data valid. Fetched Characters:"+
- for _, v in pairs(data) do +
- d(v.name) +
- end +
- return data +
- end +
- end+
  
- local function failed(error,header,statuscode+      local data = json.decode(str
- d("HTTP Failed Error"..error+      if data then 
- d("HTTP Failed Header: "..header) +         d("HTTP Requestdata validCurrency Coin info:"
- d("HTTP Failed StatusCode: "..tostring(statuscode)) +         d(data) 
- d(str or "Updating Characters failedbut nothing returned.") +      end 
- end+       
 +      local function HeadersTable(header) 
 +         if type(header) == "stringand #header > 0 then 
 +            header = string.match(header,".?%c(.*)"-- Removing the first entry 
 +            local tbl = {} 
 +            for w in header:gmatch("[^%c]+") do 
 +               local k,v = string.match(w,"(.*): (.*)"
 +               tbl[k] = v 
 +            end 
 +            table.print(tbl) 
 +            return tbl 
 +         end 
 +      end 
 +       
 +      header = HeadersTable(header) -- if you want to convert the header string to a table 
 +   end
  
- local params = {  +   local function failed(error, header, statuscode) 
- host = "api.guidwars2.com",  +      d("HTTP Failed Error: " .. error) 
- path = "/v2/characters?ids=all&access_token=.. API_Key,  +      d("HTTP Failed Header: " .. header) 
- port = 443,  +      d("HTTP Failed StatusCode: " .. tostring(statuscode)) 
- method = "GET", //"GET" or "POST" +   end 
- https = true + 
- body = "",  +   local params = { 
- headers = "", +      host = "api.guildwars2.com", 
- onsuccess = success,  +      path = "/v2/currencies?ids=1", 
- onfailure = failed,  +      port = 443, 
- getheaders = true // if true, headers will be returned +      method = "GET", -- "GET","POST","PUT","DELETE" 
-+      https = true
- HttpRequest(params)+      onsuccess = success, 
 +      onfailure = failed, 
 +      getheaders = true, --true will return the headersif you dont need it you can leave this at nil 
 +      body = "", --optional, if not required for your call can be nil or "" 
 +      headers = {}, --optional, if not required for your call can be nil or "" 
 +   
 + 
 +   HttpRequest(params)
 end end
 </code> </code>
minionlib.txt · Last modified: 2021/04/30 18:13 by ace