http client POST method using arduino libraries

Gtracks
Posts: 5
Joined: Sun Jun 25, 2017 8:12 pm

http client POST method using arduino libraries

Postby Gtracks » Sun Jun 25, 2017 8:20 pm

Hi Everybody,

We are trying to push some data from the esp32 to the cloud using the httpclient post method without success. What we see on the server side is that parts of the http header gets filled (user agent and IP address), but the json data package arrives empty (cgi FieldStorage empty). I tested the cloud setup by sending post requests using a script with the same IP as the esp32 so I am pretty sure its OK. Here is the code to build the data json data package:

Code: Select all

void make_packet(){
  packet = "{"; // JSON dictionary to send
  // Build up json datum variable by variable:
  for(uint8_t i=0; i < (UPDATE_SIZE); i++)
  {
   packet.concat( "x" + String(i) + ": " + String(data_update[i]));
   if( i < (UPDATE_SIZE-1) ){
      packet.concat(", ");
   }
  }
  packet.concat(" }");
  }
 
Here is the code to assemble and send the post request:

Code: Select all

HTTPClient WiFi_TCP::http;     // http client definition
String WiFi_TCP::packet  = " ";  

void WiFi_TCP::Handle_Client()
{
   http.begin(WiFi_TCP::http_server);
   http.setUserAgent( WiFi_TCP::our_user_agent );
   http.addHeader("Host", "our_host.com");
   http.addHeader("Content-Type", "application/json");
   make_packet();
   int httpCode = http.POST(packet);
   #ifdef SERIAL_DEBUG
      Serial.printf("Post Response code: %d\n", httpCode);
      if( httpCode <= 0 ){
         Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }
   #endif
   http.end();
}
We would appreciate it if anybody can see what we are doing wrong if anybody has experience getting the HTTPClient POST method to work. By the way, we have also tried building up the headers without using the addHeader methods but they came up empty in that case.

Who is online

Users browsing this forum: No registered users and 156 guests