2

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

3
  • what is the value of http_code? Commented Jun 19, 2023 at 17:58
  • It starts off as 400. And then -1 for the subsequent requests. The 400 error is the output text I provided above. Commented Jun 19, 2023 at 19:21
  • Its always best to add more information to the question itself. Commented Jun 21, 2023 at 1:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.