Page 1 of 1

Do I have to http.begin/http.end for every http.post?

Posted: Wed Jan 23, 2019 1:12 pm
by Pcborges
Hi, I have a data collection sketch that stores data (in the form of SQL formatted commands) in a text file to be posted at midnight.
I am looking for some help from someone with experience in this sort of arrangements in order to save some development/testing time.
Currently I am doing as follows:

Code: Select all

    while(file){
      dataBuffer = SD.open(file.name(), FILE_READ);
      while (dataBuffer.available()) {   
        //open channel
        HTTPClient http;   
        http.begin(URLtxt);  
        
        //send request + data
        http.addHeader("Content-Type", "application/x-www-form-urlencoded");
        SQLtxt = dataBuffer.readStringUntil('\n');
        _post = "query=" + urlencode(SQLtxt);
        httpResponseCode = http.POST(_post); 
        
        ..... check if response is ok or not
        
       http.end()
      }
    }
I would like to make the process more efficient and potentially faster by opening and closing http only once .
Something like:

Code: Select all

    while(file){
      dataBuffer = SD.open(file.name(), FILE_READ);
      //open channel
      HTTPClient http;   
      http.begin(URLtxt); 
     //send request + data
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");      
      while (dataBuffer.available()) {   
           SQLtxt = dataBuffer.readStringUntil('\n');
           _post = "query=" + urlencode(SQLtxt);
           httpResponseCode = http.POST(_post); 
                  
           ..... check if response is ok or not    
      }
      http.end()      
    }
Sorry for the generic code but the original sketch is 1000+ lines long and I hope this simplification will be enough to get the idea.

Thanks in advance.
Paulo

Re: Do I have to http.begin/http.end for every http.post?

Posted: Fri Jan 25, 2019 7:45 pm
by chegewara

Re: Do I have to http.begin/http.end for every http.post?

Posted: Sat Jan 26, 2019 9:55 pm
by Pcborges
Thanks
Paulo