Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Created August 17, 2020 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/0b3cfa0abb528ad8d118ca16789a4870 to your computer and use it in GitHub Desktop.
Save cosminpopescu14/0b3cfa0abb528ad8d118ca16789a4870 to your computer and use it in GitHub Desktop.
Simple curl wrapper for C++
#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);
}
#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();
};
@cosminpopescu14
Copy link
Author

use like this
CurlWrapper myCurlWrapper{ CURLOPT_URL, CURLOPT_FOLLOWLOCATION, CURLOPT_WRITEFUNCTION, CURLOPT_WRITEDATA }; fprintf(stderr, "myCurlWrapper: -- %d", myCurlWrapper.GetResponse());

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