arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
288 stars 172 forks source link

Unable to send custom Headers to a post #16

Closed mathcoll closed 7 years ago

mathcoll commented 7 years ago

Hello,

I'm trying to post data to a server using ArduinoHttpClient. in the loop(), I have added :

    client.beginRequest();
    client.sendHeader("User-Agent: My Custom UA");
    client.sendHeader("Accept: application/json");
    client.sendHeader("Content-Type: application/json");
    client.sendHeader("Authorization: Bearer " + String(Bearer));
    client.sendHeader("Content-Length", postData.length());
    client.post("/api/index.php", contentType, postData);
    client.endRequest();

Unfortunately, the server got the following headers from the request:

{ host: '192.168.0.15:3000',
  'user-agent': 'Arduino/2.2.0',
  connection: 'close',
  'content-type': 'application/json',
  'content-length': '104' }

Could you please advice? is there anything I'm doing wrong?

sandeepmistry commented 7 years ago

Hi @mathcoll,

Please have a look at Massimo's sketch: https://create.arduino.cc/editor/askmassimo/feb3bd6f-2c8c-4eba-892e-3612eaa999bd/preview

The post must be called after beginRequest, followed by the custom headers:

  client.beginRequest();
  client.post("/v1.1/messages"); //, contentType, buf
  client.sendHeader("Authorization", AuthorizationData);
  client.sendHeader("Content-Type", "application/json");
  client.sendHeader("Content-Length", len);
  client.endRequest();
  client.print(but);