Executing Sequential HTTP Requests on ESP32

im currently working on a project with an ESP32 where I need to execute two HTTP requests sequentially:

first, I fetch data from a specific URL, and then I ping the same URL with additional parameters to confirm that esp procesed it.

Here's what my code looks like:


  HTTPClient http;

  // Fetch data from the server
  if (http.begin("http://example.com/1.php") && http.GET() == 200) {
    String data = http.getString();
    http.end(); 

    HTTPClient http;
    http.begin("http://example.com/1.php?confirmed=1");
    http.end();
  }

but on second time i get http status -1 ( i checked server and it has no problem)

so im wonder if i need to type HTTPClient http; 2 times and do i need to use http.end() just once at end

just use one instance, but call end() twice since it's a different request.

  HTTPClient http;
  bool success = false;
  String data;

  if (http.begin("http://example.com/1.php")) {
    if (http.GET() == 200) {
      data = http.getString(); 
      success = true;
    }
    http.end(); 
  } 
  
  if (success && http.begin("http://example.com/1.php?confirmed=1")) http.end();

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