JosephP91 / curlcpp

An object oriented C++ wrapper for CURL (libcurl)
https://josephp91.github.io/curlcpp
MIT License
630 stars 174 forks source link

CURLOPT_POSTFIELDS changing variable data #122

Closed Meqolo closed 5 years ago

Meqolo commented 5 years ago

When using curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fdata);,the variable appears to be changed from the orginal data (not explained very well).

For example:

CURL *curl;
        CURLcode res;
        std::string content;
        struct WriteThis wt;
        wt.readptr = data;
        wt.sizeleft = strlen(data);
        res = curl_global_init(CURL_GLOBAL_DEFAULT);
        struct curl_slist *chunk = NULL;
        curl = curl_easy_init();

        std::string header_string;

        if (Cookie.empty()) {
            return "You must be logged in";
        }
        if (Xcsrf.empty()) {
            Http::UpdateXcsrf();
        }
        if (curl) {
            std::string chead = "Cookie: " + Cookie;
            std::string xhead = "X-CSRF-TOKEN: " + Xcsrf;
            chunk = curl_slist_append(chunk, chead.c_str());
            chunk = curl_slist_append(chunk, xhead.c_str());
            chunk = curl_slist_append(chunk, "Content-Type: application/json");
            //std::string newdat = curl_easy_escape(curl, fdata.c_str(), fdata.size());
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
            curl_easy_setopt(curl, CURLOPT_POST, 1L);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fdata);
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
            curl_easy_setopt(curl, CURLOPT_READDATA, &wt);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
            curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string);

            curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)wt.sizeleft);
            res = curl_easy_perform(curl);

            std::cout << header_string << endl;
            /* always cleanup */
            curl_easy_cleanup(curl);

        }
        curl_global_cleanup();

will return: {"errors":[{"code":0,"message":"Invalid Request, user Ids cannot be null."}]}

whereas if fdata is replaced with "{'userIds': [93082239]}", ie:

CURL *curl;
        CURLcode res;
        std::string content;
        struct WriteThis wt;
        wt.readptr = data;
        wt.sizeleft = strlen(data);
        res = curl_global_init(CURL_GLOBAL_DEFAULT);
        struct curl_slist *chunk = NULL;
        curl = curl_easy_init();

        std::string header_string;

        if (Cookie.empty()) {
            return "You must be logged in";
        }
        if (Xcsrf.empty()) {
            Http::UpdateXcsrf();
        }
        if (curl) {
            std::string chead = "Cookie: " + Cookie;
            std::string xhead = "X-CSRF-TOKEN: " + Xcsrf;
            chunk = curl_slist_append(chunk, chead.c_str());
            chunk = curl_slist_append(chunk, xhead.c_str());
            chunk = curl_slist_append(chunk, "Content-Type: application/json");
            //std::string newdat = curl_easy_escape(curl, fdata.c_str(), fdata.size());
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
            curl_easy_setopt(curl, CURLOPT_POST, 1L);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{'userIds': [93082239]}");
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
            curl_easy_setopt(curl, CURLOPT_READDATA, &wt);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
            curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string);

            curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)wt.sizeleft);
            res = curl_easy_perform(curl);

            std::cout << header_string << endl;
            /* always cleanup */
            curl_easy_cleanup(curl);

        }
        curl_global_cleanup();

will return: {"userPresences":[{"userPresenceType":1,"lastLocation":"Website","placeId":null,"rootPlaceId":null,"gameId":null,"universeId":null,"userId":93082239,"lastOnline":"2019-04-28T12:16:13.6049489Z"}]} (the expected outcome)