cURL / Mailing Lists / curl-library / Single Mail

curl-library

How to retrieve webpage from curl_easy_perform

From: Josh Angstadt <josh_at_softgenetics.com>
Date: Tue, 03 Mar 2015 11:00:15 -0500

Hi, I am having some trouble getting proper results using
curl_easy_perform. I have a specific webpage I need results from, but
what is returned is different. This is my code so far:

     {
     ...
         CURL *curl;
         std::string spath =
"https://mutalyzer.nl/json/checkSyntax?variant=AB026906.1:c.274del";
         const char *path = spath.c_str();
         char *result;
         CURLcode res;
         curl_easy_setopt(curl, CURLOPT_URL, path);
         curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP
| CURLPROTO_HTTPS);
         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
         curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
         curl_easy_setopt(curl, CURLOPT_READDATA, result);
         curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER);
         std::cout << "START PERFORM" << std::endl;
         res = curl_easy_perform(curl);
         if(res != CURLE_OK)
         {
             std::cout << "error" << std::endl;
             std::cout << "res = '" << res << "'" << std::endl;
         }
         else
         {
             std::cout << "success" << std::endl;
             std::string s(result);
             std::cout << "result = '" << s << "'" << std::endl;
             curl_easy_cleanup(curl);
         }
     ...
     }

     static size_t read_callback(void *ptr, size_t size, size_t nmemb,
void *stream)
     {
         curl_off_t nread;
         char *r = (char*)ptr;
         size_t retcode = fread(r, size, nmemb, (FILE *)stream);
         nread = (curl_off_t)retcode;
         return retcode;
     }

At some point before the if/else statement after the call to
curl_easy_perform, the result from the webpage (what I need) is printed
as output, and then the "success" print statement prints. However, I
cannot figure out how to retrieve the contents of the page that prints
before "success". I thought using the char * variable and setting it
with the READDATA would save it, but when I print the string version of
what is saved in the char * I get a bunch of seemingly random symbols.
Also I put print statements in the read_callback function but they are
never printed, but when I comment out the line setting READFUNCTION no
result is returned, so I'm pretty sure it uses it. How can I save the
result of the webpage to a usable variable?

PS: This is the exact output I get from running the code:
START PERFORM
{"valid": true, "messages": []}success

result = 'ø¹'

The portion '{"valid": true, "messages": []}' is the correct contents of
the webpage, and what I need to retrieve. I cannot figure out where is
printing this or how to save it.

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-03-03