3
votes

Our application need to use Lua to fetch some data from website.But the website require authentication(like google's login method).

I'm trying to use the LuaSocket library, but I can't find a full example of code, so I'm only half-aware of what I need to be doing.

I know that there's a second, optional parameter to http.request() that allows me to send POST data, and that I can also use the full syntax to specify the POST method and what data to send, but I have no idea what format the data should be in, and what it should look like. Table? String? What?

I'm aware as well that I also need to be setting the content type and content length - but I've no idea what those values should be, nor how to find them out. I don't know what options and what writing.

Could someone help me. Give me a full sample sign in google using lua?

Any help with that would be vastly appreciated. Thanks very much.

1
what have you tried so far? Check some other questions in luasocket tag - hjpotter92

1 Answers

3
votes

If the site does not use a HTTP basic auth, but uses a HTML form to autentificate user, and you have no access to the site developers, the best way to figure out what is going on is to peek at what browser does.

Fire up your Firebug or Google Chrome Developer Tools, or some HTTP debugging proxy.

Open the site in your browser, log in, and look at what requests browser did to do that, and what were the site's replies. You have to imitate same requests in your program.

Note that most likely the website will require you to send session information in subsequent requests to keep authenticated. It may be a cookie (or several) and / or a GET parameter. Again, look at what browser does and imitate.

As for format — search for examples on the web, there are a few.

Update: OK, here is an example.

Note that URL used in example will expire soon. Just create your own at http://requestb.in/. Open http://requestb.in/vbpkxivb?inspect in browser to see what data your program sent. Do not send real login and password to this service!

require 'socket.http'

local request_body = [[login=user&password=123]]

local response_body = { }

local res, code, response_headers = socket.http.request
{
  url = "http://requestb.in/vbpkxivb";
  method = "POST";
  headers = 
  {
    ["Content-Type"] = "application/x-www-form-urlencoded";
    ["Content-Length"] = #request_body;
  };
  source = ltn12.source.string(request_body);
  sink = ltn12.sink.table(response_body);
}

print("Status:", res and "OK" or "FAILED")
print("HTTP code:", code)
print("Response headers:")
if type(response_headers) == "table" then
  for k, v in pairs(response_headers) do
    print(k, ":", v)
  end
else
  -- Would be nil, if there is an error
  print("Not a table:", type(response_headers))
end
print("Response body:")
if type(response_body) == "table" then
  print(table.concat(response_body))
else
  -- Would be nil, if there is an error
  print("Not a table:", type(response_body))
end
print("Done dumping response")

Expected output:

Status: OK
HTTP code:      200
Response headers:
date    :       Sat, 23 Jun 2012 07:49:13 GMT
content-type    :       text/html; charset=utf-8
connection      :       Close
content-length  :       3
Response body:
ok

Done dumping response