Send Get to website with lua
Asked Answered
A

2

5

i'm having a trouble with lua.

I need send a request to a website by GET and get the response from website.

Atm all i have is this:

local LuaSocket = require("socket")
client = LuaSocket.connect("example.com", 80)
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\n\r\n")
while true do
  s, status, partial = client:receive('*a')
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()

What should i do to get the response from server ?

I want to send some info to this website and get the result of page.

Any ideas ?

Actuality answered 4/7, 2014 at 13:30 Comment(1)
What does print(s or partial) return? If it is printing nil, then your connection was not established. Otherwise, s contains the response from the server (including header info).Ormandy
A
5

I solved the problem passing the header

local LuaSocket = require("socket")
client = LuaSocket.connect("example.com", 80)
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\nHost: example.com\r\n\r\n")
while true do
  s, status, partial = client:receive('*a')
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()
Actuality answered 5/7, 2014 at 19:7 Comment(0)
Z
8

This probably won't work as *a will be reading until the connection is closed, but in this case the client doesn't know how much to read. What you need to do is to read line-by-line and parse the headers to find Content-Length and then after you see two line ends you read the specified number of bytes (as set in Content-Length).

Instead of doing it all yourself (reading and parsing headers, handling redirects, 100 continue, and all that), socket.http will take care of all that complexity for you. Try something like this:

local http = require("socket.http")
local body, code, headers, status = http.request("https://www.google.com")
print(code, status, #body)
Zoila answered 4/7, 2014 at 17:17 Comment(0)
A
5

I solved the problem passing the header

local LuaSocket = require("socket")
client = LuaSocket.connect("example.com", 80)
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\nHost: example.com\r\n\r\n")
while true do
  s, status, partial = client:receive('*a')
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()
Actuality answered 5/7, 2014 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.