Guest User

Untitled

a guest
Oct 5th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.87 KB | None | 0 0
  1. local curl = require "cURL"
  2.  
  3. local function data_writer(context, data)  
  4.   table.insert(context, data)
  5.   return #data
  6. end
  7.  
  8. local function print_response(easy, context)
  9.   local code = easy:getinfo_response_code()
  10.   local body = table.concat(context)
  11.   print(code .. "\r\n" .. body)
  12. end
  13.  
  14. local function clear_context(context)
  15.   count = #context
  16.   for i = 0, count do context[i] = nil end
  17. end
  18.  
  19. local function main()
  20.  
  21.   local tmp = {}
  22.  
  23.   local e = curl.easy()  
  24.    
  25.   -- e:setopt_verbose(true)
  26.    
  27.   e:setopt_writefunction(data_writer, tmp)
  28.  
  29.   e:setopt_url("https://httpbin.org/get")
  30.   clear_context(tmp) -- !!!
  31.   assert(e:perform())
  32.  
  33.   print_response(e, tmp)
  34.  
  35.   -- e:setopt_forbid_reuse(true)
  36.  
  37.   e:setopt_url("https://httpbin.org/ip")
  38.   clear_context(tmp) -- !!!
  39.   assert(e:perform())
  40.  
  41.   print_response(e, tmp)
  42.  
  43.   e:close()
  44. end
  45.  
  46. main()
Advertisement
Add Comment
Please, Sign In to add comment