Page 1 of 1

NEWBIE Unable to use POST with JSON with ESP32 on ARDUINO

Posted: Wed Aug 19, 2020 7:09 am
by DRI1966
Hello,

Sorry for my newbie question.

I have a small project to send and adjust the volume automatically depending the surrounding noise.
I have been watching many videos on this topic and reading a lot of information on GOOGLE without any success.

I do not think it is so difficult since it works POSTMAN and REST CLIENT, it is just me that can not translate it in ARDUINO.

This works in POSTMAN and REST CLIENT , I receive a code 200 back.

PUT http://192.168.100.34:8080/BeoZone/Zone ... aker/Level HTTP/1.1
Content-Type: application/json

{
"level":30
}



I have try this :

  1. if(WiFi.status()== WL_CONNECTED){
  2.  
  3.    HTTPClient http;  
  4.  
  5.    http.begin("http://192.168.100.34:8080/BeoZone/Zone/Sound/Volume/Speaker/Level/");
  6.    http.addHeader("Content-Type", "application/json");          
  7.    int httpResponseCode = http.PUT("level,30");  
  8.    if(httpResponseCode>0){
  9.  
  10.     String response = http.getString();  
  11.  
  12.     Serial.println(httpResponseCode);
  13.     Serial.println(response);          
  14.  
  15.    }else{
  16.  
  17.     Serial.print("Error on sending PUT Request: ");
  18.     Serial.println(httpResponseCode);
  19.  
  20.    }
  21.  
  22.    http.end();
  23.  
  24.  }else{
  25.     Serial.println("Error in WiFi connection");
  26.  }
  27.  
  28.   delay(10000);
  29. }
I receive this error :
500
{"error":{"type":"SERVER_ERROR","message":"JSON error: The document root must be either object or array. (@ offset=0): 2. Json[0:20]: level,30"}}




If I add HTTP/1.1 at the end , I receive a ERROR 400

http://192.168.100.34:8080/BeoZone/Zone ... aker/Level HTTP/1.1

400
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.</p>
</body></html>


Thank you in advance for your help.
Best Regards,
Didier

Re: NEWBIE Unable to use POST with JSON with ESP32 on ARDUINO

Posted: Wed Aug 19, 2020 8:08 am
by boarchuz

Code: Select all

"level,30"
That's not valid json. Use the same body as in the working request.

Re: NEWBIE Unable to use POST with JSON with ESP32 on ARDUINO

Posted: Wed Aug 19, 2020 1:21 pm
by martinayotte

Code: Select all

"level,30"
to be a valid JSON, it should be instead :

Code: Select all

"\"level\":30"

Re: NEWBIE Unable to use POST with JSON with ESP32 on ARDUINO

Posted: Thu Aug 20, 2020 6:38 am
by DRI1966
THANK YOU FOR YOU PROMPT REPLY :)

This works !!!

Best Regards,
Didier