ESP 8266 Multiple Get calls on same Client?

Solution for ESP 8266 Multiple Get calls on same Client?
is Given Below:

I’m trying to make Multiple GET calls using the same client. I’ve tried multiple things. New Variables, New instances. Closing Clients. The second Stock Call doesnt go through ever. It fails at “while (client.connected())” on the second Call. It Doesnt get to returning headers.

Any Ideas?

Code look like this

void loop()
{
    
    WiFiClientSecure client;
//Stock
      String payload;
    // Use WiFiClientSecure class to create TLS connection
    Serial.print("connecting to ");
    Serial.println(host);
    Serial.printf("Using fingerprint '%s'n", fingerprint);
    client.setFingerprint(fingerprint);

    if (!client.connect(host, httpsPort))
    {
        Serial.println("connection failed");
        client.stop();
    
        //return;
    }

//AAPL

    String url = "/api/v1/quote?symbol=AAPL&token=XXXX";
    Serial.print("requesting URL: ");
    Serial.println(url);

    client.print(String("GET ") + url + " HTTP/1.0rn" +
                 "Host: " + host + "rn" +
                 "User-Agent: BuildFailureDetectorESP8266rn" +
                 "Connection: closernrn");

    Serial.println("request sent");
    while (client.connected())
    {
        String line = client.readStringUntil('n');
//        String line = client.readStringUntil('r');
        Serial.println(line);
        if (line == "r")
        {
            Serial.println("headers received");
            payload = client.readStringUntil('r');
            
            //break;
        }
    }
    Serial.println("payload");
    Serial.println(payload);
    // String line = client.readString();
    //JsonObject& obj = doc.parseObject(payload);
    StaticJsonDocument<200> doc;   
    DeserializationError error = deserializeJson(doc, payload);

    // Test if parsing succeeds.
    if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
//    return;
      client.stop();
    }

//{"c":23.35,"d":-0.73,"dp":-3.0316,"h":24.6986,"l":23.25,"o":24.54,"pc":24.08,"t":1627588802}
    double currentprice = doc["c"];
    double pctchg = doc["d"];
      
    // Print values.
    Serial.println(currentprice);
    Serial.println(pctchg);    
    Serial.println("closing connection");
    client.stop();
    delay(20000);
//SPY
    String spyurl = "/api/v1/quote?symbol=SPY&token=XXXX";
    Serial.print("requesting URL: ");
    Serial.println(spyurl);

    client.print(String("GET ") + spyurl + " HTTP/1.0rn" +
                 "Host: " + host + "rn" +
                 "User-Agent: BuildFailureDetectorESP8266rn" +
                 "Connection: closernrn");

    Serial.println("request sent");
    String spypayload;


  
    while (client.connected())
    {
        String spyline = client.readStringUntil('n');
//        String line = client.readStringUntil('r');
        Serial.println(spyline);
        if (spyline  == "r")
        {
            Serial.println("headers received");
            spypayload = client.readStringUntil('r');
            //break;
        }
    }
    Serial.println("spypayload");
    Serial.println(spypayload);
    // String line = client.readString();
    //JsonObject& obj = doc.parseObject(payload);

    StaticJsonDocument<200> spydoc;   
    DeserializationError spyerror = deserializeJson(spydoc, spypayload);

    // Test if parsing succeeds.
    if (spyerror) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(spyerror.f_str());
//    return;
      client.stop();
    }

//{"c":23.35,"d":-0.73,"dp":-3.0316,"h":24.6986,"l":23.25,"o":24.54,"pc":24.08,"t":1627588802}
    double SPYcurrentprice = spydoc["c"];
    double SPYpctchg = spydoc["d"];
      
    // Print values.
    Serial.println(SPYcurrentprice);
    Serial.println(SPYpctchg);    
    Serial.println("closing connection");
    client.stop();
    

I’m an idiot.

Needed to keep the connection open on the first call

“Connection: Keep-Alivernrn”);