ESP32 HTTPS POST request failed

Following up this thread:

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 ?

#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
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmM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QYJKoZIhvcNAQELBQADggEBAHer
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);
}

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?

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 ?

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()'.

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

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

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