diff --git a/a8/curl.cc b/a8/curl.cc index 25ea454..fa729a0 100644 --- a/a8/curl.cc +++ b/a8/curl.cc @@ -15,9 +15,20 @@ namespace a8 return size * nmemb; } - bool Post(const char* url, std::string& response) + bool Post(const char* url, const std::string& content, std::string& response, const char* headers) { - return true; + CURL *curl = curl_easy_init(); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_HEADER, 0); + curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_ReceiveResponse); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); + bool ok = curl_easy_perform(curl) == CURLE_OK; + curl_easy_cleanup(curl); + return ok; } bool Get(const std::string& url, std::string& response, int timeout=10) @@ -32,5 +43,6 @@ namespace a8 curl_easy_cleanup(curl); return ok; } + } } diff --git a/a8/curl.h b/a8/curl.h index 4aa68fd..925da2a 100644 --- a/a8/curl.h +++ b/a8/curl.h @@ -5,7 +5,8 @@ namespace a8 { namespace http { - bool Post(const char* url, std::string& response); + bool Post(const char* url, const std::string& content, std::string& response, + const char* headers = nullptr); bool Get(const std::string& url, std::string& response, int timeout=10); } } diff --git a/a8/xobject.cc b/a8/xobject.cc index 2e52469..d157d21 100644 --- a/a8/xobject.cc +++ b/a8/xobject.cc @@ -250,6 +250,15 @@ namespace a8 } } + void XObject::ToUrlEncodeStr(std::string& data) + { + if (type_ == a8::XOT_OBJECT) { + for (auto& pair : *value_.object_value) { + data.append("&" + pair.first + "=" + a8::UrlEncode(pair.second->AsXValue().GetString())); + } + } + } + void XObject::JsonValueToXObject(Json::Value& json_val, a8::XObject& xobject) { Json::ValueType val_type = json_val.type(); diff --git a/a8/xobject.h b/a8/xobject.h index 5c22f02..1baa3f9 100644 --- a/a8/xobject.h +++ b/a8/xobject.h @@ -43,6 +43,7 @@ namespace a8 bool ReadFromXmlFile(const std::string& filename); bool ReadFromXmlString(const std::string& xmldata); void ToJsonStr(std::string& data); + void ToUrlEncodeStr(std::string& data); private: void JsonValueToXObject(Json::Value& json_val, a8::XObject& xobject);