Problem with HTTPS request on esp8266

Hi,

I have a program running on an esp8266 that attempts to fetch json data from an HTTPS endpoint that is hosted on aws amplify (data comes from a next.js app).

I can retrieve the data correctly when I use WifiClient alone, but when I want to use HttpClient, I get this error:

[HTTP] GET... failed, error: connection failed

This is my code:

void Network::get_data()
{
    if (consumption_data)
        return;

    const char *endpoint = "https://.....";

    wifi_client.setInsecure();
    http_client.setTimeout(10000);

    if (!http_client.begin(wifi_client, endpoint))
    {
        Serial.println("Connection failed");
    }

    // Make the HTTP request
    String payload;
    int http_code = http_client.GET();

    if (http_code == HTTP_CODE_OK)
    {
        payload = http_client.getString();
    }

    else
    {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http_client.errorToString(http_code).c_str());
        return;
    }

    Serial.print("Payload: ");
    Serial.println(payload);

    // Parse JSON response
 }

My class contains these:

    WiFiClientSecure wifi_client;
    HTTPClient http_client;

The endpoint is accessible from postman and doesn't require any auth headers. Any idea what the problem may be?

Thank you

Which exact library are you using? With that exact name I know only the library from the ESP32 core but you wrote you use an ESP8266. If you're using that library, did you check that you don't have to provide the root certificate for your site?

Thank you for your reply.

I am using this library: ESP8266HTTPClient.h

I didn't provide the object with my site's root certificate but I thought that using the setInsecure() function on my wifi_client meant that I didn't have to.

You're right, I missed that line.

The error you get is only returned if a connection to port 443 on the target host isn't possible. That either means you specified a wrong URL, have no WiFi connectivity or your WiFi network has no Internet connection.

Try the connectivity yourself: connect a PC to the same WiFi network and enter the following command:

telnet <hostname of URL> 443

where the string in <> has to be replaced by the hostname in the URL of your sketch.
If that returns an error you have to fix your network.

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