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

前端 未结 2 1230
余生分开走
余生分开走 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: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 = "Play5"
    > get_text(msg, "", "")
    Play
    > get_text(msg, "", "")
    Play5
    

    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 = "Play5"
    > get_text(msg)
    Play5
    > get_text(msg, nil, '')
    Play
    > get_text(msg, '')
    5
    > get_text(msg, '', '')
    Play
    

提交回复
热议问题