Created
August 17, 2020 05:38
-
-
Save cosminpopescu14/0b3cfa0abb528ad8d118ca16789a4870 to your computer and use it in GitHub Desktop.
Simple curl wrapper for C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "CurlWrapper.h" | |
#include <ostream> | |
CurlWrapper::CurlWrapper(CURLoption _options...) | |
{ | |
this->req = curl_easy_init(); | |
this->options = _options; | |
curl_easy_setopt(req, _options, "https://jsonplaceholder.typicode.com/todos");// just to test | |
} | |
CURLcode CurlWrapper::GetResponse() | |
{ | |
if (req == nullptr) | |
{ | |
fprintf(stderr, "req is null"); | |
} | |
this->res = curl_easy_perform(req); | |
return res; | |
if (res != CURLE_OK) | |
{ | |
fprintf(stderr, "curl_easy_operation() failed : %s\n", curl_easy_strerror(res)); | |
} | |
} | |
std::ostream& operator<<(std::ostream& out, const CurlWrapper& CurlWrapper) | |
{ | |
out << CurlWrapper; // for example | |
return out; | |
} | |
CurlWrapper::~CurlWrapper() | |
{ | |
curl_easy_cleanup(req); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "curl/curl.h" | |
#pragma once | |
class CurlWrapper | |
{ | |
private: | |
CURL* req; | |
CURLoption options, params; // options for curl | |
CURLcode res; | |
public: | |
CurlWrapper(CURLoption _options...); | |
CURLcode GetResponse(); | |
~CurlWrapper(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use like this
CurlWrapper myCurlWrapper{ CURLOPT_URL, CURLOPT_FOLLOWLOCATION, CURLOPT_WRITEFUNCTION, CURLOPT_WRITEDATA }; fprintf(stderr, "myCurlWrapper: -- %d", myCurlWrapper.GetResponse());