0

I am testing downloading a large file (2.7 KB) from a URL my github repository. I wanted to test downloading the file in chunks of 100 bytes and writing it to an SPIFFS file. I basically want to emulate the following Python code in C++:

import requests
url = "https://link.to.big.file"
response = requests.get(url, stream=True)
for chunk in response.iter_content(chunk_size=100):
    with open(filename, "wb") as f:
        f.write(chunk)

So far I have the following function going:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <FS.h>

int downloadFile(const char* url, const char* fileName, size_t chunkSize)
{
   // assume wifi connected
    std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
    client->setInsecure();
    HTTPClient https;
    https.begin(*client, url); // connect to server over HTTPS
    https.GET(); // make get request
    SPIFFS.begin(); // initialize file system
    File file = SPIFFS.open(fileName, "w") // create a file to write the downloaded data to in chunks of 100
    
    // download in chunks and write to file??
    return 0; // on success
}

I have left out the error checking statements for brevity. I have looked into the HTTPClient documentation here, but could not find what i was looking for. A small code example would really help. I can adapt it to my use case. Thanks!

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
First User
  • 704
  • 5
  • 12
  • why? just use `client.write(file);` – Juraj Jun 03 '23 at 16:46
  • > `emulate the following Python code in C++` Sure you want to open/close the receiving file every 100 bytes? http/https usually sends data in bigger packets BTW. – datafiddler Jun 04 '23 at 13:25
  • 100 bytes was just an arbitrary figure. AFAIK the esp8266 has 40KB of RAM, so anything strictly less than that could also serve as the chunk size for this particular demonstration. – First User Jun 04 '23 at 20:04
  • Hi, @Juraj thanks for the tip, revisiting this question after the long time. What finally did it for me was the Example called "StreamHttpsClient". – First User Jul 25 '23 at 20:59

0 Answers0