【发布时间】:2018-02-26 00:08:39
【问题描述】:
我自己执行 http.post() 请求时遇到问题。 我想使用 Lua 语言在基于 ESP8266 的 NodeMCU 上执行 API 请求。 我遇到的第一个问题是“HTTPS 地址上的纯 HTTP”。 现在它说“bad token”,所以,这意味着他没有收到我的帖子参数。
如何正确?
http.post("http://www.2level.1level/api.php","Content-Type: text/plain","token=mytokenhere&arg1=argumentforrequest",
function(code, data)
if (code < 0) then
print("HTTP request failed")
else
print(code, data)
end
end)
通常我使用 GMod Lua 来发出请求。那里的代码很容易:
http.Post("https://www.2level.1level/api.php",{token=mytokenhere,arg1=argumentforrequest},function(txt) end,function(txt) end)
================== 更新。我自己编写了代码。
function ghttp.Post(url, parameters, onSuccess, onFailure, headers)
local add = ""
if parameters then
local temp = {}
for k,v in pairs(parameters) do
table.insert(temp,ghttp.encode(k).."="..ghttp.encode(v))
end
add = "?"..table.concat(temp, "&")
end
http.post(url..add,"Content-Type: application/json\r\n"..(headers or ""),"",function(code, data)
if (code < 0) then
if onFailure then onFailure() end
else
if onSuccess then onSuccess(code,data) end
end
end)
end
但现在我遇到了新问题: 一些 API 的请求仅 HTTPs 连接。
【问题讨论】:
-
该内容类型不是文本/纯文本。
-
这算是解决了吗?