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

Pcborges
Posts: 37
Joined: Thu Aug 09, 2018 9:56 pm

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

Postby Pcborges » Wed Jan 23, 2019 1:12 pm

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


Pcborges
Posts: 37
Joined: Thu Aug 09, 2018 9:56 pm

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

Postby Pcborges » Sat Jan 26, 2019 9:55 pm

Thanks
Paulo

Who is online

Users browsing this forum: No registered users and 100 guests