To come in

Our application should use Lua to extract some data from the website. But the website requires authentication (e.g. google login method).

I am trying to use the LuaSocket library, but I can’t find a complete code example, so I only partially realize what I need to do.

I know that there is a second optional parameter 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 don’t have to think in what format the data should be, and what he should look like. Table? Line? What?

I also know that I also need to adjust the type of content and the length of the content, but I do not know what these values ​​should be and how to find them. I do not know what options and what they write.

Can someone help me. Give me a complete sample sign in google using lua?

Any help with this would be greatly appreciated. Many thanks.

+4
source share
1 answer

If the site does not use basic HTTP authentication, but uses an HTML form to authenticate the user, and you do not have access to the developers of the site, the best way to find out what is happening is to look in which browser.

Launch Firebug or Google Chrome Developer Tools or an HTTP proxy.

Open the site in your browser, log in and see what requests the browser made for this, and what the answers on the site answered. You must imitate the same queries in your program.

Please note that, most likely, the website will require you to send session information in subsequent requests in order to maintain authentication. This may be a cookie (or several) and / or GET. Once again, let's see what the browser does and emulates.

As for the format - searching for examples on the Internet, there are several.

Update: OK, here is an example.

Please note that the URL used in the example will expire soon. Just create your own http://requestb.in/ . Open http://requestb.in/vbpkxivb?inspect in a browser to find out what data your program transmitted. Do not send a real username and password for 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 Result:

  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
+3
source

All Articles