Lua发送json请求正文

时间:2015-10-31 20:27:13

标签: php json http lua nodemcu

我是LUA的新手,我正试图从我的ESP8266发送一个json帖子使用LUA到我本地主机上的PHP服务器,我搜索了互联网,我无法找到任何一个例子来做任何一个可以帮助我?

我的LUA代码

-- tested on NodeMCU 0.9.5 build 20141222...20150108
-- sends connection time and heap size to http:server.php
wifi.setmode(wifi.STATION)
wifi.sta.config("VIVA-4G-LTE-6134","VIVA176429")
--wifi.sta.config("AndroidAP","rapz4736")

print('httpget.lua started')
Tstart  = tmr.now()

conn = nil
conn = net.createConnection(net.TCP, 0) 

-- show the retrieved web page

conn:on("receive", function(conn, payload) 
                       success = true
                       print(payload) 
                       end) 

-- once connected, request page (send parameters to a php script)

conn:on("connection", function(conn, payload) 
                       print('\nConnected') 
                       conn:send("POST /server.php?"
                        .."name=mometto"
                        .."&age=27"
                        .." HTTP/1.1\r\n" 
                        .."Host: 172.0.0.1\r\n" 
                        .."Connection: close\r\n"
                        .."Accept: */*\r\n" 
                        .."User-Agent: Mozilla/4.0 "
                        .."(compatible; esp8266 Lua; "
                        .."Windows NT 5.1)\r\n" 
                        .."\r\n") 
                   --  conn:send("what":"books", "count":3 )
                       end) 

-- when disconnected, let it be known
conn:on("disconnection", function(conn, payload) print('\nDisconnected') end)

conn:connect(80,'192.168.43.181') 

这里我很容易发送参数,但是当我想发送请求正文时,我试图添加此代码来发送请求正文

conn:send("what":"books", "count":3 )

但它不起作用 我收到这条消息: enter image description here

那么任何人都能为我提供任何帮助吗?

1 个答案:

答案 0 :(得分:1)

所以,首先,这是一个字典的无效Lua代码。其次,如果要发送JSON,则需要使用cjson模块对其进行编码。

尝试类似

的内容
local msg = {"what":"books", "count":3}
conn:send(cjson.encode(msg))
相关问题