NEWBIE Unable to use POST with JSON with ESP32 on ARDUINO

DRI1966
Posts: 2
Joined: Wed Aug 19, 2020 5:49 am

NEWBIE Unable to use POST with JSON with ESP32 on ARDUINO

Postby DRI1966 » Wed Aug 19, 2020 7:09 am

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

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

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

Postby boarchuz » Wed Aug 19, 2020 8:08 am

Code: Select all

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

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

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

Postby martinayotte » Wed Aug 19, 2020 1:21 pm

Code: Select all

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

Code: Select all

"\"level\":30"

DRI1966
Posts: 2
Joined: Wed Aug 19, 2020 5:49 am

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

Postby DRI1966 » Thu Aug 20, 2020 6:38 am

THANK YOU FOR YOU PROMPT REPLY :)

This works !!!

Best Regards,
Didier

Who is online

Users browsing this forum: Google [Bot], lbernstone and 59 guests