2

I have a function for HTTP_POST responses but it is never called when I post to it. I get the webpage instead.

I call on it with

server.on("/",HTTP_POST,response);

But I only get the url in response.

void setup() {
    pinMode(led_pin, OUTPUT);

    Serial.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_USB) || defined(SERIAL_PORT_USBVIRTUAL)
    delay(2000); // To be able to connect Serial monitor after reset and before first printout
#endif
    // Just to know which program is running on my Arduino
    Serial.println(F("START " __FILE__ " from " __DATE__));
    Serial.print(F("Ready to send IR signals at pin "));
    Serial.println(IR_SEND_PIN);

    pinMode(led_pin, OUTPUT);
    digitalWrite(led_pin, 0);
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("");

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    if (MDNS.begin("esp32")) {
      Serial.println("MDNS responder started");
    }

    server.on("/", handleRoot);
    server.on("/test.svg", drawGraph);
    server.on("/",HTTP_POST,response);
    server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
    });
    server.onNotFound(handleNotFound);
    server.begin();
    Serial.println("HTTP server started");

}



void response(){
  //int khz = 38; // 38kHz carrier frequency for the NEC protocol
  
  if(server.hasArg("on") && (server.arg("on").length()>0)){ // TODO check that it's not longer than 31 characters
      Serial.print("Server was turned on:\t");
      Serial.println(server.arg("on"));

      ++CYCLE0;
      server.sendHeader("Location", String("/"), true); //how to do a redirect, next two lines
      server.send ( 302, "text/plain", "");
  } else if (server.hasArg("off") && (server.arg("off").length()>0)){
      Serial.print("Server was turned off:\t");
      Serial.println(server.arg("off"));

      ++CYCLE1;
      server.sendHeader("Location", String("/"), true);
      server.send ( 302, "text/plain", "");

   } else if (server.hasArg("up") && (server.arg("up").length()>0)){
      Serial.print("Server temp was turned up:\t");
      Serial.println(server.arg("up"));

      ++CYCLE2;
      server.sendHeader("Location", String("/"), true);
      server.send ( 302, "text/plain", "");

  } else if (server.hasArg("down") && (server.arg("down").length()>0)){
      Serial.print("Server temp was turned down:\t");
      Serial.println(server.arg("down"));

      ++CYCLE3;
      server.sendHeader("Location", String("/"), true);
      server.send ( 302, "text/plain", "");
  
  } else {
    server.send(400, "text/html", "<html><body><h1>HTTP Error 400</h1><p>Bad request. Please enter a value.</p></body></html>");
  }
}

How do you get an http post response back instead of a webpage?

POST response:

<html>

<head>
    <meta http-equiv='refresh' content='5' />
    <title>ESP32 Demo</title>
    <style>
        body {
            background-color: #cccccc;
            font-family: Arial, Helvetica, Sans-Serif;
            Color: #000088;
        }
    </style>
</head>

<body>
    <h1>Hello from ESP32!</h1>
    <p>Uptime: 00:05:32</p> <img src="/test.svg" />
    <h2>Turn air condistioner on</h2>
    <p>AC has been cycled 0 times</p>
    <form name='frm0' method='post'> <input type='text' name='on' value=8000>
        <input type='submit' value='Submit'>     </form>
        <p>Turn air conditioner off 0 times</p>
        <form name='frm1' method='post'> <input type='text' name='off' value=8000>
            <input type='submit' value='Submit'>     </form>
            <p>Turn temperature up 0 times</p>
            <form name='frm2' method='post'> <input type='text' name='up' value=8000>
                <input type='submit' value='Submit'>     </form>
                <p>Turn temperature down 0 times</p>
                <form name='frm3' method='post'> <input type='text' name='down' value=8000>
                    <input type='submit' value='Submit'>     </form>
</body>

</html>

Complete code

long version

2

You have:

server.on("/", handleRoot);
server.on("/test.svg", drawGraph);
server.on("/",HTTP_POST,response);

The first call using handleRoot species no particular method, and in this case it seems any method is acceptable. You later specify server.on("/",HTTP_POST,response); specifically for POST requests. These are added to a request handler list in the order given. And apparently consulted in the order given.

In short, you need to specify HTTP_GET for handleRoot or just specify your HTTP_POST for response first and let handleRoot catch non-HTTP_POST requests afterwards:

server.on("/",HTTP_POST,response);
server.on("/", handleRoot);
server.on("/test.svg", drawGraph);
1
  • That was it. For some reason it still return the URL instead of the response get returned to the client, but I definitely see the post request hit. I set both btw server.on("/",HTTP_POST,response); server.on("/",HTTP_GET, handleRoot);
    – brad
    Feb 1 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.