I want to redirect the output of a curl request to a file, but what I did doesn't work. Can anyone help me to understand what am I doing wrong?

I've been looking every topic that could provide an answer to my problem, but unfortunately everything I tried simply doesn't work.

I used the examples from the official documentation, and other codes provided by users but nothing works for me: the output file remains empty.

Here's the last code I tried:


    std::string filename = "answer.out";
    curlpp::Cleanup cleaner;
    curlpp::Easy request;

    std::list<std::string> header; 
    header.push_back("Connection: keep-alive");
    header.push_back("Upgrade-Insecure-Requests: 1");
    header.push_back("User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 OPR/60.0.3255.83");
    header.push_back("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
    header.push_back("Accept-Encoding: gzip, deflate");
    header.push_back("Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7");
    header.push_back("Cookie: __test=4558ed530ffeb24f11c7d1812f85b68d; PHPSESSID=efb4f11bd458ed43cefb51be871dbb73");

    request.setOpt(new curlpp::options::HttpHeader(header)); 

    curlpp::options::WriteFunctionCurlFunction
        myFunction(WriteCallback); 

    FILE *file = stdout;
    if(filename != "")
    {
        file = fopen(filename.c_str(), "wb");
        if(file == NULL)
        {
          fprintf(stderr, "%s/n", strerror(errno));
        }
    }


    curlpp::OptionTrait<void *, CURLOPT_WRITEDATA> 
        myData(file);

    request.setOpt(myFunction);
    request.setOpt(myData);


    curlpp::Forms formParts;
    formParts.push_back(new curlpp::FormParts::Content("pseudo_connect", "myusername"));
    formParts.push_back(new curlpp::FormParts::Content("pass_connect", "mypassword"));

    request.setOpt(new curlpp::options::HttpPost(formParts)); 

    request.setOpt(new curlpp::options::Url("http://fpvp.fiddlecomputers.fr?action=connect"));
    request.setOpt(new curlpp::options::Verbose(false));
    request.setOpt(new curlpp::options::Encoding(""));
    request.perform();

WriteCallBack function:

size_t WriteCallback(char* ptr, size_t size, size_t nmemb, void *f)
{
    FILE *file = (FILE *)f;
    return fwrite(ptr, size, nmemb, file);
};

I don't get any errors, but the output file (ie. answer.out) remains empty. Thanks in advance for your help.

0 Answers