Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用?key=value&;发出正确的HTTP Post请求;键=地址后的值_Http_Lua_Esp8266_Nodemcu - Fatal编程技术网

如何使用?key=value&;发出正确的HTTP Post请求;键=地址后的值

如何使用?key=value&;发出正确的HTTP Post请求;键=地址后的值,http,lua,esp8266,nodemcu,Http,Lua,Esp8266,Nodemcu,我自己执行http.post()请求时遇到问题。 我想用Lua语言在基于ESP8266的NodeMCU上执行API请求。 我遇到的第一个问题是“HTTPS地址上的纯HTTP”。 现在它说的是“坏令牌”,所以,这意味着他没有收到我的帖子参数 它需要如何正确 http.post("http://www.2level.1level/api.php","Content-Type: text/plain","token=mytokenhere&arg1=argumentforrequest",

我自己执行http.post()请求时遇到问题。 我想用Lua语言在基于ESP8266的NodeMCU上执行API请求。 我遇到的第一个问题是“HTTPS地址上的纯HTTP”。 现在它说的是“坏令牌”,所以,这意味着他没有收到我的帖子参数

它需要如何正确

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)

================== 使现代化我自己编写代码

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
函数ghttp.Post(url、参数、onSuccess、onFailure、标题)
本地添加=“”
如果参数那么
本地温度={}
对于k,v成对(参数)do
表.插入(温度,ghttp.encode(k)“=”.ghttp.encode(v))
终止
add=“?”.表.混凝土(温度,&”)
终止
http.post(url..add,“内容类型:application/json\r\n”。(标题或“”),“”函数(代码,数据)
如果(代码<0),则
如果是onFailure,则onFailure()结束
其他的
如果onSuccess,则onSuccess(代码、数据)结束
终止
(完)
终止
但现在我有了新的问题:
一些API只请求HTTPs连接。

您没有给我们一个URL进行验证(我知道,这很难,因为令牌)。因此,未经测试的正确代码如下:

http.post('https://www.2level.1level/api.php',
  'Content-Type: application/json\r\n',
  '{token=mytokenhere,arg1=argumentforrequest}',
  function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
    end
  end)
http.post('https://www.2level.1level/api.php',
'内容类型:application/json\r\n',
“{token=mytokenhere,arg1=argumentforrequest}”,
功能(代码、数据)
如果(代码<0),则
打印(“HTTP请求失败”)
其他的
打印(代码、数据)
终止
(完)
通过使用例如jsonlint.com进行验证,确保您的
{token=mytokenhere,arg1=argumentforrequest}
是有效的JSON


如果您遇到HTTPS问题,那么这可能是。

该内容类型不是文本/纯文本。这可以被视为解决了吗?您的解决方案不起作用。{“status”:“NO”,“message”:“bad token”}——这意味着站点没有收到任何参数。我通过让我自己发号施令来解决问题“让我自己发号施令”——这是什么意思?我的代码的正确性可以很容易地通过运行它来验证。
http.post('https://www.2level.1level/api.php',
  'Content-Type: application/json\r\n',
  '{token=mytokenhere,arg1=argumentforrequest}',
  function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
    end
  end)