0

I'm working on a simple REST server. I made method to handle http://<esp8266_IP>/test. The GET method works like a charm and I got the response. Then I tried to send a POST with a JSON data (I used PostMan) but I cannot read anything.

Searching online I read ESP8266 library cannot parse JSON and in that case I can found plain text into server.arg("plain"). I tried that but no success

How can I read the data into the POST method?

ESP8266WebServer server(80);
...

void handleTest()
{
    if (server.method() == HTTP_GET)
    {
        ...
    }
    else if (server.method() == HTTP_POST)
    {
        String message = "POST\nHeaders:\n";
        for (uint8_t i = 0; i < server.headers(); i++)
        {
            message += server.headerName(i) + ": " + server.header(i) + "\n";
        }
        message += "\nRequest params:\n";
        for (uint8_t i = 0; i < server.args(); i++){
            message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
        }
        server.send(202, "text/plain", message);
    }  
}
...
void setup()
{
    WiFi.begin(ssid, password);
    Serial.println("");

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

    if (MDNS.begin("esp8266")) {
        Serial.println(F("[Server] MDNS responder started"));
    }

    server.on("/test", handleTest);
}
  • 2
    Please show your code. – Mark Smith Jun 16 '17 at 18:10
  • i'm not sure it can do that, aside from uploading files... – dandavis Jun 17 '17 at 1:10
  • @MarkSmith I don't think it will be usefull, btw I paste a section of code in my question. – Noisemaker Jun 17 '17 at 7:25
1

Instead of using below in handleTest()

else if (server.method() == HTTP_POST)

try using below in setup()

server.on("/test", POST, handleTest);
| improve this answer | |

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.