Web server POST form

I’m making a web server and I want to send e-mail and password with a form using method POST. When I’m clicking a Submit button I’m getting an error ERR_CONNECTION_RESET but with GET method this works.

            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<head>");
            client.println("<meta charset=\"utf-8\" />");
            client.println("<title>Test</title>");
            client.println("</head>");
            client.println("<body>");
            client.println("<div class=\"container\">");
            client.println("<center><h1>myECG</h1></center>");
            client.println("
");
            client.println("<form action=\"/\" method=\"POST\">  <input id=\"email\" type=\"text\" class=\"form-control\" name=\"email\" placeholder=\"E-mail\">   <input id=\"password\" type=\"password\" class=\"form-control\" name=\"password\" placeholder=\"Password\"><input type=\"submit\" class=\"btn btn-info center-block\" value=\"Submit\"></form>");
            client.println("</div>");
            client.println("</body>");
            client.println("</html>");
            client.stop();

That piece of code doesn't say anything, it's only the server's response to the GET request. Where's the code that handles the POST request?

Post all of your code.

Pieter

Well I don't really know how POST handler should look like

What did you try? Where did you get the GET handler code?

Give some more details: what board and libraries are you using. Post your full code anyway, that gives us a place to start. No-one is going to write complete code for you.

I’m using Intel Edison but it can be programmed just like Arduino. Here is my full code:

#include <Ethernet.h>
#include <Wire.h>

EthernetServer server(8084);

void setup() {
  Serial.begin(9600);
  Ethernet.begin(NULL);
  server.begin();
}

String process_query(EthernetClient& client)
{
  String get_url;
  String line;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      Serial.write(c);
      line += c;

      if (line == "\r\n")
        return get_url;
      if (c == '\n') {
        if (line.startsWith("GET ")) {
          get_url = line.substring(4);
          int pos = get_url.indexOf(' ');
          if (pos != -1) {
            get_url = get_url.substring(0, pos);
          }
          get_url.trim();
        }
        line = "";
      }
    }
  }
}

void send_index(EthernetClient& client) {

            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<head>");
            client.println("<meta charset=\"utf-8\" />");
            client.println("<title>myECG</title>");
            client.println("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\">");
            client.println("</head>");
            client.println("<body>");
            client.println("<div class=\"container\">");
            client.println("<center><h1>myECG</h1></center>");
            client.println("
");
            client.println("  <form action=\"/\" method=\"POST\">  <div class=\"container\">   <div class=\"row\">     <div class=\"col-sm-offset-3 col-sm-6\">      <div class=\"input-group\">         <span class=\"input-group-addon\"><i class=\"fa fa-user\"></i></span>         <input id=\"email\" type=\"text\" class=\"form-control\" name=\"email\" placeholder=\"E-mail\">       </div>    
      <div class=\"input-group\">         <span class=\"input-group-addon\"><i class=\"fa fa-lock\"></i></span>         <input id=\"password\" type=\"password\" class=\"form-control\" name=\"password\" placeholder=\"Password\">       </div>  
  <center>    <input type=\"submit\" class=\"btn btn-info center-block\" value=\"Log In\">  </center>   </div>  </div>  </div>  </form>");
            client.println("</div>");
            client.println("</body>");
            client.println("</html>");
            client.stop();

}

void send_error(EthernetClient& client)
{
  client.println("HTTP/1.1 404 Not Found");
  client.println();
}

void loop() {
  EthernetClient client = server.available();
  
  if (client) {
    String url = process_query(client);
    if (url == "/")
      send_index(client);
    else
      send_error(client);
    client.stop();
  }
}

This is your clue:

if (line.startsWith("GET ")) { /* ... */ }

Study HTTP requests, and find out how to support not only GET requests but POST requests as well. Look up some HTTP articles online to get a general idea of what's going on. Then if you want to implement it, consult the RFCs (RFC 7230, RFC 7231, RFC 7232, RFC 7233, RFC 7234, RFC 7235).

The formal BNF specification is extremely useful when implementing a parser.

The code you posted isn't very robust, and you'll have to use some tricks to get it to support POST as well (because it has a body, and you actually have to read the headers).

That being said, there's no real point in reinventing the wheel here. There are many Arduino HTTP parsers out there, most of them are open-source. So find one you like, and if it's not supported on your platform, you can port it yourself. If you're pleased with the result of your port, open a pull request on GitHub to contribute.