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://
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.
That library has a BasicHttpsClient example, showing how the Secure client is passed as the first argument 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.
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.
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.