httpclient post method

apoyoman
Posts: 2
Joined: Sun Jun 25, 2017 4:27 pm

httpclient post method

Postby apoyoman » Sun Jun 25, 2017 5:02 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.

bbx10node
Posts: 11
Joined: Thu Sep 29, 2016 5:25 am

Re: httpclient post method

Postby bbx10node » Thu Jun 29, 2017 5:55 am

Try a trivial JSON string. I think you need the double quotes around the name as well as the value assuming you sending a string.

Code: Select all

   int httpCode = http.POST("{\"name\":\"value\"}");

apoyoman
Posts: 2
Joined: Sun Jun 25, 2017 4:27 pm

Re: httpclient post method

Postby apoyoman » Thu Jun 29, 2017 3:54 pm

Thanks. That is right, in order to be a proper json string the "\"name\"" must be quoted as you so did. The other problem was the structure of the FieldStorage arriving at the server. Since we sent it a key value pair, it seems like

Code: Select all

f = cgi.FieldStorage()
should contain the key-value pairs we want. It does, but I found you have to peal it a little further to uncover the pairs. Here is the code that I used to do the pealing:

Code: Select all

        f = cgi.FieldStorage(keep_blank_values=True,environ={'REQUEST_METHOD':'POST'})
        for key in f.keys():
                if is_json(key):
                        mdict = json.loads(key)
                        for k in mdict.keys():
                                v = mdict[k]
                                data.k = v
What this code implies is that the data does not arrive in key value pairs at the upper level. In fact it is just a list, the first two items of the list are blank and if you dont set keep_blank_values = True, you just get three blank fields. The "key" of the last item contains the key value pairs we packed in the json package.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 78 guests