Skip to content

Instantly share code, notes, and snippets.

@whoshuu
Created March 31, 2015 06:44
Show Gist options
  • Save whoshuu/2dc858b8730079602044 to your computer and use it in GitHub Desktop.
Save whoshuu/2dc858b8730079602044 to your computer and use it in GitHub Desktop.
Example libcurl GET request
#include <curl/curl.h>
#include <string>
size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*) ptr, size * nmemb);
return size * nmemb;
}
int main(int argc, char** argv) {
auto curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/whoshuu/cpr/contributors?anon=true&key=value");
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0");
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
std::string response_string;
std::string header_string;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string);
char* url;
long response_code;
double elapsed;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl = NULL;
}
}
@azhidkov
Copy link

Small note, if you want to get proper values in url, response_code and elapsed you need call curl_easy_getinfo methods after curl_easy_perform(curl);

@hack-tramp
Copy link

hack-tramp commented Aug 2, 2020

this worked for me (using the global init and cleanup was needed):

#include <iostream>
#include <curl/curl.h>
#include <string>

using namespace std;

size_t writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
    data->append((char*)ptr, size * nmemb);
    return size * nmemb;
}

int main(int argc, char** argv) {
    curl_global_init(CURL_GLOBAL_DEFAULT);
    auto curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com");
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

        std::string response_string;
        std::string header_string;
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
        curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string);

        curl_easy_perform(curl);
        cout << response_string;
        curl_easy_cleanup(curl);
        curl_global_cleanup();
        curl = NULL;
    }
}

@jumpredhhyh
Copy link

jumpredhhyh commented Oct 22, 2020

I had to change "size_t writeFunction" to "static size_t writeFunction" in order to stop getting the following error: "non-standard syntax; use '&' to create a pointer to member"; however, aside from that, this example is working perfectly.

@danielkoohmarey-bc
Copy link

danielkoohmarey-bc commented Aug 12, 2021

On linux systems you can build the example with g++ -o curl_get curlget.cpp -lcurl

@emadflash
Copy link

Small note, if you want to get proper values in url, response_code and elapsed you need call curl_easy_getinfo methods after curl_easy_perform(curl);

yep it did

@Anton-V-K
Copy link

If curl_easy_perform fails with the code CURL_SSL_CACERT, consider adding curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE), but beware this makes your app less secure, and the proper solution is to set CURLOPT_CAINFO

@eyalroz
Copy link

eyalroz commented Jul 19, 2022

Suggest you switch the condition, writing:

if (not curl) { exit 1; } // or EXIT_FAILURE

so the rest of the code doesn't need the indentation and our brain stack can be empty.

@anonymgit
Copy link

can any body help me to run this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment