Skip to content

Instantly share code, notes, and snippets.

@mr5z
Created November 17, 2016 09:14
Show Gist options
  • Save mr5z/2e51f44f38a1526b3447490c4167b4b8 to your computer and use it in GitHub Desktop.
Save mr5z/2e51f44f38a1526b3447490c4167b4b8 to your computer and use it in GitHub Desktop.
class Http {
public:
// omitted other code
/**
* @param callback(CURLcode, response)
*/
template<typename F>
void request(std::string url, F &callback) {
curl_easy_reset(handle);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &writeCallback);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
CURLcode res = curl_easy_perform(curl);
callback(res, response);
}
private:
size_t writeCallback(void *contents, size_t size, size_t nmemb, std::string *data) {
response.append((char*)contents, size * nmemb);
return size * nmemb;
}
private:
CURL *handle;
std::string response;
static int curlInstanceCount;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment