-->
Page 1 of 1

Error on http GET request

PostPosted: Thu Mar 31, 2016 10:06 pm
by johnnyo
Been trying a variety of methods to create an http get request. This is my current code:
37 -- create http client
38 conn=net.createConnection(net.TCP, 0)
39 conn:on("receive", function(conn, payload) print(payload) end)
40 conn.connect(80,HOST)
41 conn.send("GET " .. param.. "HTTP/1.1\r\n")
42 conn:send("Host: "..HOST.."\r\n")
43 conn.send("Accept: */*\r\n")
44 conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
45 conn:send("\r\n")
46 conn:on("sent",function(conn)
47 conn:close()
48 end)

The code fails with "file.lua:40: Bad argument #1 to 'connect' (net.socketexpected, go number). It's almost a straight copy out of documentation from a variety of forums, so I'm really scratching my head as to why it's failing.

Re: Error on http GET request

PostPosted: Sat Apr 02, 2016 8:06 am
by Geert
I cannot test it right now, but I had the same problem before, keep in mind that the HOST can only be an IP adress in your example, not a domainname. Also I saw that i have to use conn:on("connection",... to wait for the connection to be established before sending data to the server...

I hope it helps...

Code: Select all 
--Create a TCP protocol object
conn=net.createConnection(net.TCP, 0)
-- When you recieve data from the object 'conn' print this to the serial port
 conn:on("receive", function(conn, payload) print(payload) end)
-- Create a connection on this IP adress
 conn.connect(80,"216.58.208.142")
conn:on("connection", function(conn, c)
   conn.send("GET " .. param.. "HTTP/1.1\r\n")
   conn:send("Host: "..HOST.."\r\n")
   conn.send("Accept: */*\r\n")
   conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
   conn:send("\r\n")
  end)
conn:on("sent",function(conn)
conn:close()
 end)