void _SetPostOptions(const std::string& url, const std::string& type, const CurlOptions& options) { CURLcode err; err = curl_easy_setopt(handle_, CURLOPT_CUSTOMREQUEST, type.c_str()); _CheckError(err, "set request type"); _SetCommonOptions(url); err = curl_easy_setopt(handle_, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL); _CheckError(err, "set post redir"); std::ostringstream ostr; for (auto const& opt :options) { ostr << opt.first << '=' << opt.second << ';'; } std::string opts (ostr.str()); if (! opts.empty()) { err = curl_easy_setopt(handle_, CURLOPT_POST, 1L); _CheckError(err, "set post"); err = curl_easy_setopt(handle_, CURLOPT_COPYPOSTFIELDS, opts.c_str()); _CheckError(err, "set copy post fields"); } }
// OPERATIONS std::string Get(const std::string& url) { _ResetHandle(); _SetGetOptions(url); CURLcode err = curl_easy_perform(handle_); _CheckError(err, "easy perform"); return write_stream_.str(); }
std::string Set(const std::string& url, const std::string& type, const CurlOptions& options) { _ResetHandle(); _SetPostOptions(url, type, options); CURLcode err = curl_easy_perform(handle_); _CheckError(err, "easy perform"); return write_stream_.str(); }
void _SetCommonOptions(const std::string& url) { // set url CURLcode err = curl_easy_setopt(handle_, CURLOPT_URL, url.c_str()); _CheckError(err, "set url"); // Allow redirection err = curl_easy_setopt(handle_, CURLOPT_FOLLOWLOCATION, 1L); _CheckError(err, "set follow location"); // Clear write stream write_stream_.str(""); write_stream_.clear(); // Set callback for write err = curl_easy_setopt(handle_, CURLOPT_WRITEFUNCTION, _WriteCb); _CheckError(err, "set write callback"); // Set callback data err = curl_easy_setopt(handle_, CURLOPT_WRITEDATA, this); _CheckError(err, "set write data"); if (enable_header_) { // Get curl header // clear existing header data header_stream_.str(""); header_stream_.clear(); // Set header callback function err = curl_easy_setopt(handle_, CURLOPT_HEADERFUNCTION, _HeaderCb); _CheckError(err, "set header callback"); // Set header user data for callback function err = curl_easy_setopt(handle_, CURLOPT_HEADERDATA, this); _CheckError(err, "set header data"); } // Set the user agent. Some servers requires this on requests err = curl_easy_setopt(handle_, CURLOPT_USERAGENT, "libcurl-agent/1.0"); _CheckError(err, "set write data"); }
void _Parse(const std::string& json) { document_.Parse(json.c_str()); _CheckError(); }