# ESP32 HTTPS POST request

**URL:** <https://forum.arduino.cc/t/esp32-https-post-request/964599>\
**Category:** Networking, Protocols, and Devices\
**Created:** [March 1, 2022, 8:56am UTC](https://forum.arduino.cc/t/esp32-https-post-request/964599 "2022-03-01T08:56:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![steflipkens7300](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/steflipkens7300/32/746940_2.png) [@steflipkens7300](https://forum.arduino.cc/u/steflipkens7300)\
**Post date:** [March 1, 2022, 8:56am UTC](https://forum.arduino.cc/t/esp32-https-post-request/964599/1 "2022-03-01T08:56:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martinius96](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/martinius96/32/246870_2.png) [@martinius96](https://forum.arduino.cc/u/martinius96)\
**Post date:** [March 1, 2022, 11:49am UTC](https://forum.arduino.cc/t/esp32-https-post-request/964599/2 "2022-03-01T11:49:14Z")

</div>

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

```cpp
WiFiClientSecure client;

```

then I add to your code Root CA like:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/d/7/0d79e4c9a942cca4725cc81b69f4c3d51573ad22.png)

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

```cpp
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:

```cpp
client.setInsecure(); // NOT RECOMMENDED

```

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

```cpp
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"));
    }

```

---

<div class="post-metadata">

**Author:** ![steflipkens7300](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/steflipkens7300/32/746940_2.png) [@steflipkens7300](https://forum.arduino.cc/u/steflipkens7300)\
**Post date:** [March 7, 2022, 8:16am UTC](https://forum.arduino.cc/t/esp32-https-post-request/964599/3 "2022-03-07T08:16:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steflipkens7300](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/steflipkens7300/32/746940_2.png) [@steflipkens7300](https://forum.arduino.cc/u/steflipkens7300)\
**Post date:** [March 7, 2022, 11:21am UTC](https://forum.arduino.cc/t/esp32-https-post-request/964599/4 "2022-03-07T11:21:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martinius96](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/martinius96/32/246870_2.png) [@martinius96](https://forum.arduino.cc/u/martinius96)\
**Post date:** [March 7, 2022, 12:47pm UTC](https://forum.arduino.cc/t/esp32-https-post-request/964599/5 "2022-03-07T12:47:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steflipkens7300](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/steflipkens7300/32/746940_2.png) [@steflipkens7300](https://forum.arduino.cc/u/steflipkens7300)\
**Post date:** [March 7, 2022, 1:15pm UTC](https://forum.arduino.cc/t/esp32-https-post-request/964599/6 "2022-03-07T13:15:37Z")

</div>

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

---

<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:** [September 3, 2022, 1:15pm UTC](https://forum.arduino.cc/t/esp32-https-post-request/964599/7 "2022-09-03T13:15:52Z")

</div>

Closed on September 3, 2022, 1:15pm UTC

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