DaKaZ / esp8266-restclient

Arduino RESTful HTTP Request Library
MIT License
124 stars 25 forks source link

Strange behaviour #10

Open michelep opened 5 years ago

michelep commented 5 years ago

Hi,

first, thanks for your work. I have a strange behaviuor using your RestClient library as a pointer. I define a RestClient object as a pointer, because i need to create it after loading the collector host IP from a config file:

RestClient *restClient;

Later i initialize the object:

      ...
      restClient = new RestClient(config.collector_host); 
      ....

and i send data via POST like:

  char query[128];
  String response="";
  ...
          sprintf(query,"key=%s&ts=%d&t=%s&p=%s&h=%s&uv=%d&pm25=%s&pm10=%s",config.api_key,data.ts,data.temp,data.pres,data.hum,data.uv,data.pm25,data.pm10);
          statusCode = restClient->post("/rest/data/put", query, &response);

but watching data with tcpdump, i see strange behavour in header like:

POST /rest/data/put HTTP/1.1
Host: [host]:80
Connection: close
Content-Length: 36
**Content-Type: L.).**

key=KEY&t=21.60&h=39.80

Any help really appreciated !

DaKaZ commented 5 years ago

Sorry for the complete lack of response, I did not see this issue get created. I have not personally used the library with a pointer. Obviously you are getting some memory corruption. The new RestClient should properly alloc your memory. So some more questions:

  1. Are you ever calling restClient->setContentType? If you are, you might need to strdup (or we can update the library (see https://github.com/DaKaZ/esp8266-restclient/blob/master/RestClient.cpp#L145)

  2. Is the body correct there? I am assuming no since the length is reported at 36 and your body key=KEY&t=21.60&h=39.80 is only 23, but maybe you removed KEY?