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!

4
  • why? just use client.write(file); Commented Jun 3, 2023 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. Commented Jun 4, 2023 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. Commented Jun 4, 2023 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". Commented Jul 25, 2023 at 20:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.