How to get post parameters from http request in lua sent to NodeMCU

前端 未结 2 1229
余生分开走
余生分开走 2021-01-28 05:24

I sent this HTTP POST request via Tasker (Android app) to my NodeMCU, which looks like this:

POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User         


        
相关标签:
2条回答
  • 2021-01-28 06:02

    For the sake of completeness, here is another solution I came up with:

    string.gsub(request, "<(%a+)>([^<]+)</%a+>", function(key, val)
      print(key .. ": " .. val)
    end)
    

    A working example using the given HTTP request in your question can be seen here:

    https://repl.it/repls/GlamorousAnnualConferences

    0 讨论(0)
  • 2021-01-28 06:06

    This function allows you to extract text from between two string delimiters:

    function get_text (str, init, term)
       local _, start = string.find(str, init)
       local stop = string.find(str, term)
       local result = nil
       if _ and stop then
          result = string.sub(str, start + 1, stop - 1)
       end
       return result
    end
    

    Sample interaction:

    > msg = "<action>Play</action><SetVolume>5</SetVolume>"
    > get_text(msg, "<action>", "<SetVolume>")
    Play</action>
    > get_text(msg, "<action>", "</SetVolume>")
    Play</action><SetVolume>5
    

    This is a modification of the above function that allows nil for either of the parameters init or term. If init is nil, then text is extracted up to the term delimiter. If term is nil, then text is extracted from after init to the end of the string.

    function get_text (str, init, term)
       local _, start
       local stop = (term and string.find(str, term)) or 0
       local result = nil
       if init then
          _, start = string.find(str, init)
       else
          _, start = 1, 0
       end
    
       if _ and stop then
          result = string.sub(str, start + 1, stop - 1)
       end
       return result
    end
    

    Sample interaction:

    > msg = "<action>Play</action><SetVolume>5</SetVolume>"
    > get_text(msg)
    <action>Play</action><SetVolume>5</SetVolume>
    > get_text(msg, nil, '<SetVolume>')
    <action>Play</action>
    > get_text(msg, '</action>')
    <SetVolume>5</SetVolume>
    > get_text(msg, '<action>', '<SetVolume>')
    Play</action>
    
    0 讨论(0)
提交回复
热议问题