[Solved]- Photon AP Mode Form Post Request Not Working

I have been working with the Photon in AP/Listening mode and have it sending a page with a HTML page with a form that has the method set to POST. However, whenever I press the submit button from the form, nothing happens. I do have debug output so that I can monitor the data as the Photon receives HTML requests.

I am able to get standard page requests and get requests, but post requests in particular don’t seem to work (the loading of the page just pauses and never finishes).

Here is the page that gets sent:

const char* formpostpage =
R"(<!DOCTYPE html>
<html>
<head>
<title>Form Test Page</title>
</head>
<body>

<form enctype="text/plain" id="testFormId" method="post" name="testForm" action="/get.html">
<p style="text-align: center;">This is the form testing page for the POST request<br />
<input name="txtTestbox" type="text" value="This is a textbox" /><br />
<input name="btnTestbut" type="submit" value="POST Submit" /></p>
</form>

</body>
</html>)";

And here is the handler for the HTTP requests:

void myPage(const char* url, ResponseCallback* cb, void* cbArg, Reader* body, Writer* result, void* reserved)
{
// Ensure that our Serial is working
if(!serialBegun)
{
    serialBegun = true;
    Serial.begin(BAUD_RATE);
}

Serial.println("HTTP REQUEST");

// Make index equal to index.html
if (strcmp(url,"/index")==0)
{
    Header h("Location: /index.html\r\n");
    cb(cbArg, 0, 301, "text/plain", &h);
    return;
}
else
{
    // Iderate through the available pages
    int8_t idx = 0;
    for (;;idx++)
    {
        Page& p = pages[idx];
        if (!p.url) // If we reach the null page, send the 404
        {
            idx = -1;
            break;
        }
        else if (strncmp(url, p.url, strlen(p.url))==0) // If we match an url, break with the propper idx value
        //else if (((String)url).substring(0, strlen(p.url)+1) == (String)p.url)
        {
            Serial.print("Page Requested: ");
            Serial.println(url);
            break;
        }
    }

    if (idx==-1) // When we didn't find the page, send the 404
    {
        cb(cbArg, 0, 404, nullptr, nullptr);
    }
    else // Send back the page we want to send
    {
        Serial.print("Page Sent: ");
        Serial.println(pages[idx].url);
        // Get output of result
        Serial.print("Body size: ");
        Serial.println(body->bytes_left);
        
        // Try to grab data
        if (body->bytes_left) 
        {
            char* data = body->fetch_as_string(); // Grab our data
            // handle the body data
            Serial.print("Data: ");
            Serial.println(data);
            // free the data! IMPORTANT!
            free(data);
        }
        else
        {
            Serial.println("No data detected");
        }
        
        // Send our page
        cb(cbArg, 0, 200, pages[idx].mime_type, nullptr);
        result->write(pages[idx].data);
        
        Serial.print("Url: "); Serial.println(url);
    }
}
}

I was able to fix the issue by removing the enctype="text/plain" from the form.

1 Like

Thank you for posting your solution! This is a big help to people that might run into the same issue as you!