Post your best Lua script examples here

User avatar
By Mitchh22
#22474 I'm trying to use the LUA HTTP request example from: http://nodemcu.com/index_en.html#fr_547 ... 501100000f

I need to connect to a simple API and then perform action based on the response (Like change the color of the LED).

Everything works as expected only I am confused on how to capture only the Body of the response, not headers too.

Here is my script I'm running:
Code: Select allconn=net.createConnection(net.TCP, false)
conn:on("receive", function(conn, pl) print(pl) end)
conn:connect(80,"107.170.231.171")
conn:send("GET /test.html HTTP/1.1\r\nHost: mitchellharding.com\r\n"
    .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")



Here is the output from ESPlorer:
Code: Select all conn=net.createConnection(net.TCP, false)
> conn:on("receive", function(conn, pl) print(pl) end)
> conn:connect(80,"107.170.231.171")
> conn:send("GET /test.html HTTP/1.1\r\nHost: mitchellharding.com\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
> HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Sun, 05 Jul 2015 03:06:50 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Sat, 04 Jul 2015 21:41:38 GMT
Connection: keep-alive
ETag: "55985312-7"
X-Robots-Tag: noindex, nofollow, nosnippet, noarchive
Accept-Ranges: bytes

testing




The expected result is just 'testing'.

How can I just grab this response (will eventually be json) and set it as a variable?

Any help is most appreciated!