xrotors
xrotors

Reputation: 7

Fetching a URL with standard LUA API

I want to make http requests with LUA standard API

I tried socket.http and didn't worked, the lua framework I'm working into (I'm an add-on developer for X-Plane flight sim) won't allow me to do that. I can only use LUA API.

url = declare url here
io.open (url)

I expect the code to fetch the file into the URL

Upvotes: 0

Views: 1110

Answers (2)

Stan Sieler
Stan Sieler

Reputation: 759

The phrase "I expect the code to fetch the file into the URL" is baffling.

I presume you meant "I expect the code to fetch the contents/file/text at the URL into a local file", and will continue with that understanding ...

Your lua code has several problems

  1. The '-k".."' results in "-khttp", which is an error (you need a blank after "-k").

  2. You never read the results. The "io.popen" (when successful), runs the command as a child process (which writes to stdout, a pipe), and the text is returned as a Lua file (poorly named "file" ... a variable name like "curl_results" is more descriptive ... "file" is often a reserved word in many languages and can confuse some readers).

So, you needed (after a successful popen): s = file:read ("*a") which would read the entire result into the string 's'.

Here's a working example that fetches your outside/global/WAN IP address via the service at ipify.org:

 -- myip.lua

   -- The following fetches our external (aka "global" or "WAN")
   -- IP address via the free service from api.ipify.org
   -- (If you load that page, it returns your IP address (no newline))
   --
   -- on success: found_data is non-nil, my_ip has string 
   -- on failure: found_data is nil, my_ip is nil
   -- 
   -- curl notes: 
   --    The "-s" means "silent" 
   --    The "-k" means "insecure" (e.g., ignore any problems with
   --                                     security certificates)
   --
   -- locals: curl_data
   -- globals: found_data, my_ip  
       
       -- initialize default results (nils indicate failure)

 found_data = nil  -- will be nil or 1 
 my_ip = nil       -- will be nil or something like "192.1.2.3"

       -- use curl to read api.ipify.org ...

 local curl_data = io.popen ("curl -s -k http://api.ipify.org")

 if curl_data then    -- io.popen worked. 
    found_data = 1    -- File found  (I usually use 'true' (and do: true = 1))

    my_ip = curl_data:read ("*a")  -- read entire file into string my_ip

    if (not my_ip) or (#my_ip == 0) then
       my_ip = nil               -- in case it was an empty string
       found_data = nil          -- an empty result means we got no data
    end

    io.close (curl_data)         -- close the file (pipe)
    curl_data = nil              -- (in case not global, remove stale data)

    -- note: we did not verify that the result looks like an IP address

    -- print (my_ip)             -- uncomment for debugging/demo

 else
    found_data = nil -- File not found
    print ("no found")
 end

    -- found_data should be 1 (and my_ip has "#.#.#.#"), or 
    -- found_data and my_ip will both be nil

 if found_data then
    print ("My IP address (to the world) is: " .. my_ip)
 else
    print ("Could not find my (outside) IP address")
 end  

and, a sample use:

 lua myip.lua
 My IP address (to the world) is: 16.22.84.45

Upvotes: 1

xrotors
xrotors

Reputation: 7

That could be a great idea, unfortunately it won't work. Maybe I'm missing something syntax side.

local file = io.popen("curl -s -k".."https://myurl/myfile")
  if file then
      io.close(file)
  is_file = 1 -- File found
else
  is_file = 0 -- File not found
end

It keeps saying file is found, even it there's nothing into /myurl/

Upvotes: 0

Related Questions