yhirose / cpp-httplib

A C++ header-only HTTP/HTTPS server and client library
MIT License
13.08k stars 2.3k forks source link

form-data with large files #768

Closed MosquitoParis closed 3 years ago

MosquitoParis commented 3 years ago
string GetFileContent(string path)
{
    string content = "";
    FILE *f = fopen(path.c_str(), "rb");
    while (!feof(f))
        content += fgetc(f);
    fclose(f);

    return content;
}

Scan UploadFile(string path, string api_key)
{
    httplib::Client virustotal("www.virustotal.com");
    Scan info;

    auto response = virustotal.Post("/vtapi/v2/file/scan", httplib::MultipartFormDataItems{
        { "apikey", api_key, "", "" },
        { "file", GetFileContent(path), path, "" }
        });

    if (response)
    {
        cout << response->status << endl;
        cout << response->body << endl;

        nlohmann::json resp = nlohmann::json::parse(response->body);

        info.scan_id = get(resp["scan_id"]);
        info.resource = get(resp["resource"]);
        info.response_code = atoi(get(resp["response_code"]).c_str());
        info.permalink = get(resp["permalink"]);
        info.verbose_msg = get(resp["verbose_msg"]);

        info.hash.sha256 = get(resp["sha256"]);
        info.hash.sha1 = get(resp["sha1"]);
        info.hash.md5 = get(resp["md5"]);
    }
    else
        cout << "err" << response.error() << endl;

    return info;
}

i wanna to send file to virustotal with size 1000kb and have error 4. How can i send large files to server?

MosquitoParis commented 3 years ago

I found way to fix it. I just set bigger write and read timeout in config.

likecarrot commented 1 year ago

What is the principle that also solves my problem