-1

I'm currently using the following Arduino code on an ESP32 (I've taken out non-relevant parts) to make an https request for a binary file from a server, to store in SPIFFS. However I now need to set a custom header, and so need to use the https.beginRequest() method. But this method does not take a WiFiClientSecure reference, and so I cannot use HTTPS for this. Can the standard Arduino lib actually perform https requests with custom headers, or is there another lib for this?

  WiFiClientSecure client;
  client.setInsecure(); // Not using certificate check while testing

  HTTPClient https;
  https.useHTTP10(true);
  Serial.println("https.begin...");
  if (https.begin(client, "https://path.to.binary.file")) {  // HTTPS
    Serial.println("Sending GET request...");
    //https.sendHeader("X-device: 12345678"); // Cannot use here

    // start connection and send HTTP header
    int httpCode=https.GET();
    Serial.printf("Response code: %u\n",httpCode);
    Serial.printf("Content length: %u\n",https.getSize());
    uint32_t bytesRead=0;
    uint8_t b;
    while(client.connected()){
      while(client.available()){
       ++bytesRead;
        b=client.read();
        Serial.printf("0x%02x ",b);
        if((bytesRead%16)==0){
          Serial.println();
        }
      }
    }
    Serial.printf("\nBytes read: %u\n",bytesRead);
    https.end();
  }else{
    Serial.println("Could not connect to server");
  }

2 Answers2

0

Just adding this as the documentation is incorrect. The https.addHeader() method CAN be used before calling GET, and does work. The official documentation states that it can only be used between the beginRequest and endRequest calls.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

https.sendHeader("X-device: 12345678");

The https.sendHeader() does not add header as you think it was, it actually send out all the headers (except body) starting from GET / HTTP/1.1 all the way till the separator between http headers and http body, see the source code.

To add a custom header to the http request, you need to call http.addHeader() with

https.addHeader("X-device", "12345678");

Here is the code that should work:

void loop() {
  WiFiClientSecure client;
  client.setInsecure(); // Not using certificate check while testing

  HTTPClient https;
  https.useHTTP10(true);
  Serial.println("https.begin...");
  if (https.begin(client, "https://httpbin.org/get")) {  // HTTPS
    Serial.println("Sending GET request...");
    https.addHeader("X-device", "12345678");
    int httpCode=https.GET();
    Serial.printf("Response code: %u\n",httpCode);
    Serial.printf("Content length: %u\n",https.getSize());
    Serial.println("HTTP response:");
    Serial.println(https.getString());

    https.end();
  }else{
    Serial.println("Could not connect to server");
  }

  while(1);
}

This code will return the response from https://httpbin.org/get which basically echo back all the headers sent/added, plus client IP and request URL:

https.begin...
Sending GET request...
Response code: 200
Content length: 265
{
  "args": {}, 
  "headers": {
    "Host": "httpbin.org", 
    "User-Agent": "ESP32HTTPClient", 
    "X-Amzn-Trace-Id": "Root=1-64392c39-768ce2a95ca639bc0f79e256", 
    "X-Device": "12345678"
  }, 
  "origin": "xxx.xx.xx.xx", 
  "url": "https://httpbin.org/get"
}
hcheung
  • 3,377
  • 3
  • 11
  • 23