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);
}
VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Jun 16, 2017 at 17:59
3
  • 2
    Please show your code. Commented Jun 16, 2017 at 18:10
  • i'm not sure it can do that, aside from uploading files... Commented Jun 17, 2017 at 1:10
  • @MarkSmith I don't think it will be usefull, btw I paste a section of code in my question. Commented Jun 17, 2017 at 7:25

1 Answer 1

1

Instead of using below in handleTest()

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

try using below in setup()

server.on("/test", POST, handleTest);
Greenonline
3,1527 gold badges36 silver badges48 bronze badges
answered Jul 14, 2018 at 16:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.