2017-09-17 9 views
-3

http.post() 요청을 직접 수행하는 데 문제가 있습니다. 루아 어가있는 ESP8266을 기반으로 NodeMCU에서 API 요청을 수행하려고합니다. 내가 만난 첫 번째 문제는 "HTTPS 주소의 일반 HTTP"입니다. 지금은 "악성 토큰"이라고 말하면서, 그가 내 게시물 매개 변수를받지 못했음을 의미합니다.주소 뒤에? key = value & key = value를 사용하여 올바른 HTTP Post 요청을 만드는 방법

어떻게 정확해야합니까?

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) 

http.Post on GMod Lua Wiki

================== 업데이트. 나는 내 자신의 코드를 만들었다.

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 만 연결합니다.

+0

콘텐츠 유형이 text/plain이 아닙니다. –

+0

해결할 수 있습니까? –

답변

0

당신은 우리에게 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) 

것이 있는지 확인하여 {token=mytokenhere,arg1=argumentforrequest} 함께 확인하여 유효한 JSON입니다 예를 들어, jsonlint.com.

HTTPS에서 문제가 발생하는 경우 https://github.com/nodemcu/nodemcu-firmware/issues/1707 일 수 있습니다.

+0

해결책이 작동하지 않았습니다. { "status": "NO", "message": "bad token"} - 사이트가 인수를받지 못했음을 의미합니다. 나는 나에게 자신의 명령을 내림으로써 문제를 해결한다. – Spar

+0

"나 자신의 명령을 만든다"- 그게 무슨 뜻입니까? 내 코드의 정확성은 http://httpbin.org/post를 통해 실행하여 쉽게 확인할 수 있습니다. –

+0

질문을 업데이트합니다. – Spar