ESP32 HTTPS POST request

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...

Because Postman is adapted for that as any other program that is available for Windows or other systems (you don't see its source code so you can't say why it is working there :slight_smile: ). And it have Root CA certificates bundle available from operating system or from Google Chrome if you installed it there as extension. At ESP32 you are using I think WiFi library that is websocket without SSL, so it can be used for HTTP or MQTT connections. If you use library WiFi for HTTPS conection it will not work. Error 400 is something like: Server is awaiting handshake but your microcontroller is not doing it.

So you need to use WiFiClientSecure library that can be used for HTTPS connections. Also you need to set Root CA certificate in your sketch (that means certificate of certificate authority in .pem format that signed certificate for webserver where you are connecting to). I think there is some bundle of Root CA certificates available at ESP32 too, but I still using static certificate that I add directly to source code of my application.

So first of all I create WiFiClientSecure object before setup()

WiFiClientSecure client;

then I add to your code Root CA like:
image

Then I set certificate (in setup() function):

client.setCACert(test_root_ca);

If you don't want to use certificate (only for test purposes), then you can do one thing that is NOT recommended.
Call this function and not setCACert, also you dont need certificate in source code:

client.setInsecure(); // NOT RECOMMENDED

Then I just change 80 to 443 in original HTTP POST request that I was using previously.

client.stop();
    if (client.connect(host, 443)) {
      client.println("POST " + url + " HTTP/1.0");
      client.println("Host: " + (String)host);
      client.println(F("User-Agent: ESP"));
      client.println(F("Connection: close"));
      client.println(F("Content-Type: application/x-www-form-urlencoded;"));
      client.print(F("Content-Length: "));
      client.println(data.length());
      client.println();
      client.println(data);
      Serial.println(F("Data were sent successfully"));
        while (client.connected()) {
          String line = client.readStringUntil('\n'); //HTTP headers
          if (line == "\r") {
            break;
          }
        }
        String line = client.readStringUntil('\n'); //payload first row
    } else {
      Serial.println(F("Connection wasnt established"));
    }

First of all thanks for your response! In what type is your data? It is just a String right?

I am unable to establish a connection if I use client.setInsecure();
Same thing if I do use a certificate.

I read datas as String, in my case it was in my many apps text like: OK or similar... Also I was using JSON response in some projects to read by microcontroller, but connection part was identical in source code. It is easy to convert String to any other format...

Have you some example URL where you are trying to connect?
If it is not working with setInsecure() there is some other problem like.. not corect server name. Isnt there some specific protocol for https connection? Other than 443

I fixed it, I used HTTPClient.begin(wificlientsecure,hostname) and added the ca certificate in the setup. That did the job :slight_smile: Thanks for your help!

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