# ESP32 HTTPS POST request failed

**URL:** <https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715>\
**Category:** Networking, Protocols, and Devices\
**Created:** [January 19, 2023, 4:42pm UTC](https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715 "2023-01-19T16:42:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaus313](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/klaus313/32/1127527_2.png) [@klaus313](https://forum.arduino.cc/u/klaus313)\
**Post date:** [January 19, 2023, 4:42pm UTC](https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715/1 "2023-01-19T16:42:23Z")

</div>

Following up this thread:

> [@ESP32 HTTPS POST request](https://forum.arduino.cc/t/esp32-https-post-request/964599):
>
> Can someone explain to me why I can simply change http request to https request on postman or Node-RED but not on an ESP32? If I change my servername to something with HTTPS I get a 400 bad request...

but even with this input I still have problems to get proper data back from the server:

First I tried the connection with Curl on Windows , here all is fine and I get a JSON string back  
In Arduino the response code is 200 - supposing for me everything is ok , but the data in the String  
is just not readable: 1F8B0800000000000003EDFD478E2449D2BCFF5FA5E1EB8C82B97998477AEE9C847B98979B7B7A8487B34DC0DDDC23C29C8447BA47788479A3EFDE488FEE445DC2E13B1315151311DBFF2D0CB4BB96C287D05DCB0B2BFEBAF6DD9135A5F02694CF2BEBCBE1C02EC2878414E54DB87575...

Any idea what is going wrong ?

```arduino
#include <Arduino.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
//#include <WiFiClient.h>
#include <WiFiClientSecure.h> // ********* Server
WiFiClient client1;   

#include <time.h>

const char *MY_NTP_SERVER = "de.pool.ntp.org"; // NTP Zeit = Internetzeit   
const char *MY_TZ = "CET-1CEST,M3.5.0/02,M10.5.0/03"; // NTP Zeit = Internetzeit, Zeitzone und wann die Umstellung Winter-/Sommerzeit passiert  
  // Unfortuntely it is called configTzTime() on esp32
  // configTzTime(TZstr, "server1", "server2", "server3");
tm tm; // NTP Zeit = Internetzeit, the structure tm holds time information in a more convient way
time_t t_unixtime; // NTP Zeit = Internetzeit
bool DEBUG_TIME = true;
bool Time_valid = false;
int cnt_time_chk_max =20;

#define FORMAT_LITTLEFS_IF_FAILED true

const char root_ca[] PROGMEM = R"=====(
-----BEGIN CERTIFICATE-----
MIIE6jCCA9KgAwIBAgIQCjUI1VwpKwF9+K1lwA/35DANBgkqhkiG9w0BAQsFADBh
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
QTAeFw0yMDA5MjQwMDAwMDBaFw0zMDA5MjMyMzU5NTlaME8xCzAJBgNVBAYTAlVT
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxKTAnBgNVBAMTIERpZ2lDZXJ0IFRMUyBS
U0EgU0hBMjU2IDIwMjAgQ0ExMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
AQEAwUuzZUdwvN1PWNvsnO3DZuUfMRNUrUpmRh8sCuxkB+Uu3Ny5CiDt3+PE0J6a
qXodgojlEVbbHp9YwlHnLDQNLtKS4VbL8Xlfs7uHyiUDe5pSQWYQYE9XE0nw6Ddn
g9/n00tnTCJRpt8OmRDtV1F0JuJ9x8piLhMbfyOIJVNvwTRYAIuE//i+p1hJInuW
raKImxW8oHzf6VGo1bDtN+I2tIJLYrVJmuzHZ9bjPvXj1hJeRPG/cUJ9WIQDgLGB
Afr5yjK7tI4nhyfFK3TUqNaX3sNk+crOU6JWvHgXjkkDKa77SU+kFbnO8lwZV21r
eacroicgE7XQPUDTITAHk+qZ9QIDAQABo4IBrjCCAaowHQYDVR0OBBYEFLdrouqo
qoSMeeq02g+YssWVdrn0MB8GA1UdIwQYMBaAFAPeUDVW0Uy7ZvCj4hsbw5eyPdFV
MA4GA1UdDwEB/wQEAwIBhjAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIw
EgYDVR0TAQH/BAgwBgEB/wIBADB2BggrBgEFBQcBAQRqMGgwJAYIKwYBBQUHMAGG
GGh0dHA6Ly9vY3NwLmRpZ2ljZXJ0LmNvbTBABggrBgEFBQcwAoY0aHR0cDovL2Nh
Y2VydHMuZGlnaWNlcnQuY29tL0RpZ2lDZXJ0R2xvYmFsUm9vdENBLmNydDB7BgNV
HR8EdDByMDegNaAzhjFodHRwOi8vY3JsMy5kaWdpY2VydC5jb20vRGlnaUNlcnRH
bG9iYWxSb290Q0EuY3JsMDegNaAzhjFodHRwOi8vY3JsNC5kaWdpY2VydC5jb20v
RGlnaUNlcnRHbG9iYWxSb290Q0EuY3JsMDAGA1UdIAQpMCcwBwYFZ4EMAQEwCAYG
Z4EMAQIBMAgGBmeBDAECAjAIBgZngQwBAgMwDQYJKoZIhvcNAQELBQADggEBAHer
t3onPa679n/gWlbJhKrKW3EX3SJH/E6f7tDBpATho+vFScH90cnfjK+URSxGKqNj
OSD5nkoklEHIqdninFQFBstcHL4AGw+oWv8Zu2XHFq8hVt1hBcnpj5h232sb0HIM
ULkwKXq/YFkQZhM6LawVEWwtIwwCPgU7/uWhnOKK24fXSuhe50gG66sSmvKvhMNb
g0qZgYOrAKHKCjxMoiWJKiKnpPMzTFuMLhoClw+dj20tlQj7T9rxkTgl4ZxuYRiH
as6xuwAwapu3r9rxxZf+ingkquqTgLozZXq8oXfpf2kUCwA/d5KxTVtzhwoT0JzI
8ks5T1KESaZMkE4f97Q=
-----END CERTIFICATE-----
)=====" ;

char ssid[18], password[18];

WiFiMulti WiFiMulti;

void setup() {
  Serial.begin(115200);
                                                              // Serial.setDebugOutput(true);
  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("xxxxx", "xxxxxxxxxxx");

  Serial.print("Waiting for WiFi to connect...");
  while ((WiFiMulti.run() != WL_CONNECTED)) {
    Serial.print(".");
  }
  Serial.println(" connected");

  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  
}
void loop() {  
  if ((WiFiMulti.run() == WL_CONNECTED)) {
  /*                                          
   configTzTime(MY_TZ, MY_NTP_SERVER);  
   bool DEBUG_TIME = true;
   bool Time_valid = false;
   int cnt_time_chk_max =20;
   tm.tm_year = 70; // year = Jahr seit 1900
   int cnt_time_chk = 0;
   while(tm.tm_year <= 70 && cnt_time_chk < cnt_time_chk_max){
     cnt_time_chk++;
     time(&t_unixtime); //NTP Zeit = Internetzeit
     localtime_r(&t_unixtime, &tm); //NTP Zeit = Internetzeit, speichert die lokale Zeit in der Struktur tm  
     delay(1000); // Test weil Zeit nicht ready  
     Serial.println("[TIME] waitloop 200ms");
     Serial.printf("[TIME] in loop: %d:%dUhr %d \n\r", tm.tm_hour, tm.tm_min, (tm.tm_year+1900));
   }
   if (cnt_time_chk < cnt_time_chk_max){
     Time_valid = true;
   }else
   {
     Time_valid = false;
   }
   if (DEBUG_TIME == 1){  
        if (Time_valid==true){                                                               
           Serial.printf("[TIME] at Startup: %d:%dUhr %d \n\r", tm.tm_hour, tm.tm_min, (tm.tm_year+1900));
        }
        else{
           Serial.printf("[TIME] not valid after 120 trials");
        }
   } 
 */                                          
    WiFiClientSecure client;  
        
    client.setCACert(root_ca); 
    HTTPClient https;
    Serial.print("[HTTPS] begin...\n");
    
    if (https.begin(client, "https://idpcvs.peugeot.com/am/oauth2/access_token")) { // HTTPS
      https.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
     // https.addHeader("Accept", "application/json");
      https.addHeader("Authorization", "Basic xxxx");
      String payload = "realm=clientsB2CPeugeot&grant_type=password&password=xxxxx&username=xxxx%40xxx&scope=openid profile";
      Serial.print("[HTTPS] POST...\n");
      int httpCode = https.POST(payload);
      if (httpCode > 0) {                   
        Serial.printf("[HTTPS] POST... code: %d\n", httpCode);                            
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
        }
      }else {
          Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }
      https.end();
    } else {
          Serial.printf("[HTTPS] Unable to connect\n");
    }
  }
  while(1);
}

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 19, 2023, 4:55pm UTC](https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715/2 "2023-01-19T16:55:47Z")

</div>

> [@klaus313](#):
>
> the data in the String  
> is just not readable: 1F8B0800000000000003EDFD478E2449D2BCFF5FA5E1EB8C82B97998477AEE9C847B98979B7B7A8487B34DC0DDDC2

It looks like you are receiving the response in some sort of hexadecimal format. What do the response headers tell you about the data format?

---

<div class="post-metadata">

**Author:** ![klaus313](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/klaus313/32/1127527_2.png) [@klaus313](https://forum.arduino.cc/u/klaus313)\
**Post date:** [January 19, 2023, 5:41pm UTC](https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715/3 "2023-01-19T17:41:16Z")

</div>

ok I added  
`https.collectHeaders(headerKeys, numberOfHeaders);`

just before the POST request  
and I just got the date back

is there a way to make more visible ?

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 20, 2023, 2:20am UTC](https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715/4 "2023-01-20T02:20:53Z")

</div>

You can try printing `https.transferEncoding`. From the sources it looks like that value is saved regardless of what headers you ask for with 'collectHeaders()'.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 20, 2023, 5:14pm UTC](https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715/5 "2023-01-20T17:14:34Z")

</div>

> [@klaus313](#):
>
> ```arduino
> if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
> String payload = https.getString();
> Serial.println(payload);
> }
> 
> ```

```arduino
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          Serial.println(https.transferEncoding);
          String payload = https.getString();
          Serial.println(payload);
        }

```

---

<div class="post-metadata">

**Author:** ![klaus313](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/klaus313/32/1127527_2.png) [@klaus313](https://forum.arduino.cc/u/klaus313)\
**Post date:** [January 21, 2023, 6:24am UTC](https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715/6 "2023-01-21T06:24:07Z")

</div>

thanks, unfortunately https.transferEncoding is not available. The from compiler proposed \_transferEncoding is protected ...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 20, 2023, 6:24am UTC](https://forum.arduino.cc/t/esp32-https-post-request-failed/1079715/7 "2023-07-20T06:24:51Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
