# Using HTTP on an HTTPS server

**URL:** <https://forum.arduino.cc/t/using-http-on-an-https-server/1369111>\
**Category:** Networking, Protocols, and Devices\
**Created:** [March 31, 2025, 7:41am UTC](https://forum.arduino.cc/t/using-http-on-an-https-server/1369111 "2025-03-31T07:41:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![babski123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/babski123/32/1203223_2.png) [@babski123](https://forum.arduino.cc/u/babski123)\
**Post date:** [March 31, 2025, 7:41am UTC](https://forum.arduino.cc/t/using-http-on-an-https-server/1369111/1 "2025-03-31T07:41:28Z")

</div>

So I have an API service (Node.JS) hosted in Google Cloud Run and my esp32 sends request to this API to upload audio files captured from INMP441.

However, my Cloud Run service runs in https and when I try to send request using HTTPClient library with https:// in the URL, esp32 prints -1 response code, it returns the error immediately like in a split second.

I tried removing "s" so using only http:// in the URL, this now seems that it actually connects but still returns an error. The error printed in serial is 302.

Do I really have to add a certificate in this case to establish secure connection? Or can I just skip that part and use http://

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [March 31, 2025, 8:05am UTC](https://forum.arduino.cc/t/using-http-on-an-https-server/1369111/2 "2025-03-31T08:05:55Z")

</div>

`302` is not an error. Strictly speaking, errors with `HTTPClient` are negative integers. `302` is one of several redirects: "There is no plain HTTP service here on port 80; try this other HTTPS url on port 443" (probably)

To establish a secure connection, you need to get a copy of the publicly available 20-year root certificate, probably used to sign the 5-year intermediate certificate that in turn signed the 6-month leaf certificate currently in use by the endpoint. This is used by the client to validate that the leaf cert is actually valid, and that the endpoint is who they claim to be.

This is different than the server's role, renewing and rotating certs every six months. The client does not have to get its own cert unless it wants or needs to perform mTLS: mutual TLS, where the server and client verify each other.

The other alternative on the client end with the ESP32 is on the `NetworkClientSecure` (fka `WiFiClientSecure`): call `setInsecure`: "Don't bother to validate the certificate chain." This is **NOT RECOMMENDED**. You can try it just to see if everything else is working. Getting the root cert, and then passing it via `setCACert` is not difficult.

---

<div class="post-metadata">

**Author:** ![babski123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/babski123/32/1203223_2.png) [@babski123](https://forum.arduino.cc/u/babski123)\
**Post date:** [April 1, 2025, 4:35am UTC](https://forum.arduino.cc/t/using-http-on-an-https-server/1369111/3 "2025-04-01T04:35:33Z")

</div>

Thank you, makes sense, that would be easy when using WifiClientSecure but I am using this library to make my HTTP Requests: [arduino-esp32/libraries/HTTPClient/src/HTTPClient.h at master · espressif/arduino-esp32 · GitHub](https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/src/HTTPClient.h)

I've checked the codes and example but I can't seem to find anything that allows insecure connection.

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [April 1, 2025, 5:20am UTC](https://forum.arduino.cc/t/using-http-on-an-https-server/1369111/4 "2025-04-01T05:20:21Z")

</div>

That library has a BasicHttpsClient example, showing how the `Secure` client is [passed as the first argument](https://github.com/espressif/arduino-esp32/blob/3.1.3/libraries/HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino#L99) to `HTTPClient::begin`. In that example, both clients are created inside the `loop`, but you can also create them globally.

If you're still having problems, post the code you have.

---

<div class="post-metadata">

**Author:** ![babski123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/babski123/32/1203223_2.png) [@babski123](https://forum.arduino.cc/u/babski123)\
**Post date:** [April 1, 2025, 5:59am UTC](https://forum.arduino.cc/t/using-http-on-an-https-server/1369111/5 "2025-04-01T05:59:30Z")

</div>

Yeah, I dug deeper in the library, I saw I have to pass the secure client to the http client. I made it work with with the `BasicHttpsClient` example and added a rootCA I got from Google Trust Services.

This has been working so far and I'm seeing the error logs in my server (just sending plain post request with an empty payload) but as soon as I switch to my original code which uses same cert (only difference is the payload is a .wav file from SPIFF), I get -1 error code. My code works normally when hosted in my local network, issue happens when deployed to cloud run.

The issue might be in my code so I'll investigate further and post back here if I need more help.

---

<div class="post-metadata">

**Author:** ![babski123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/babski123/32/1203223_2.png) [@babski123](https://forum.arduino.cc/u/babski123)\
**Post date:** [April 3, 2025, 12:50pm UTC](https://forum.arduino.cc/t/using-http-on-an-https-server/1369111/6 "2025-04-03T12:50:31Z")

</div>

I can't seem to find the reason why my original code is not working.  
So I just switched to the HTTPSClient example, modified it and added my original codes to it and it is working now.

@kenb4 Thanks man!

---

<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 30, 2025, 12:50pm UTC](https://forum.arduino.cc/t/using-http-on-an-https-server/1369111/7 "2025-09-30T12:50:32Z")

</div>

Closed on September 30, 2025, 12:50pm UTC

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