int BitcoinMarketInfo::getBitcoinMarketInfo() { Curly curly; Json::Value retJSON; if (curly.Fetch(this->serviceURL) == CURLE_OK){ if (curly.HttpStatus() != 200) { return FAIL; } retJSON = curly.getContentJSON(); //std::cout << "status: " << curly.HttpStatus() << std::endl; //std::cout << "type: " << curly.Type() << std::endl; //std::vector<std::string> headers = curly.Headers(); //for(std::vector<std::string>::iterator it = headers.begin(); // it != headers.end(); it++) // std::cout << "Header: " << (*it) << std::endl; if (!retJSON.empty()) { this->setBuy(retJSON.get("ticker", "" ).get("buy", "" ).asDouble()); this->setDate(retJSON.get("ticker", "" ).get("date", "" ).asInt()); this->setHigh(retJSON.get("ticker", "" ).get("high", "" ).asDouble()); this->setLast(retJSON.get("ticker", "" ).get("last", "" ).asDouble()); this->setLow(retJSON.get("ticker", "" ).get("low", "" ).asDouble()); this->setVol(retJSON.get("ticker", "" ).get("vol", "" ).asDouble()); this->setSell(retJSON.get("ticker", "" ).get("sell", "" ).asDouble()); return SUCCESS; } } return FAIL; }
int main (int argc, const char * argv[]) { Curly curly; cout << "start" << endl; if (curly.Fetch("https://oxfordhk.azure-api.net/academic/v1.0/evaluate?expr=Id=2332023333&attributes=C.CId&subscription-key=f7cc29509a8443c5b3a5e56b0e38b5a6") == CURLE_OK){ std::cout << "status: " << curly.HttpStatus() << std::endl; std::cout << "type: " << curly.Type() << std::endl; std::vector<string> headers = curly.Headers(); for(std::vector<string>::iterator it = headers.begin(); it != headers.end(); it++) std::cout << "Header: " << (*it) << std::endl; std::cout << "Content:\n" << curly.Content() << std::endl; } return 0; }
int main() { Curly get; //set URL get.setURL("https://httpbin.org/headers"); // test headers const std::vector<std::pair<std::string, bool> > testHeaders = { { "Foo: bar", true}, { "X-Custom-Header-Test: scan-tool test suite", true}, // empty header, should fail { "", false}, // missing colon (":"), should fail { "ThisHeaderDoesNotContainAColon or does it?", false}, //has colon, but as first character, should fail { ": DotDot", false}, // CRLF not allowed, should fail { "X-Custom-Header-ABC: Hey \r\n", false}, { "X-Custom-Header-DEF: Hey \r", false}, { "X-Custom-Header-GHI: Hey \n", false} }; //try to add headers for (const auto & elem : testHeaders) { if (get.addHeader(elem.first) != elem.second) { std::cout << "Error: Header \"" << elem.first << "\" could "; if (elem.second) std::cout << "NOT be added!" << std::endl; else std::cout << " be added, although that should NOT be possible!" << std::endl; return 1; } } //for //perform request std::string response = ""; if (!get.perform(response)) { std::cout << "Error: Could not perform GET request!" << std::endl; return 1; } //check HTTP status code if (get.getResponseCode() != 200) { std::cout << "Error: HTTP status code is not 200, it is " << get.getResponseCode() << " instead!" << std::endl; return 1; } //check content type if (get.getContentType() != "application/json" && !get.getContentType().empty()) { std::cout << "Error: Content type is not application/json, it is " << get.getContentType() << " instead!" << std::endl; return 1; } //check response std::cout << "Response:" << std::endl << response << std::endl << std::endl; Json::Value root; // will contain the root value after parsing Json::Reader jsonReader; const bool success = jsonReader.parse(response, root, false); if (!success) { std::cerr << "Error: Unable to parse JSON data from response!" << std::endl; return 1; } Json::Value headers = root["headers"]; if (headers.empty() || !headers.isObject()) { std::cout << "Error: headers element in response is empty or no object!" << std::endl; return 1; } //check for "Foo: bar" Json::Value val = headers["Foo"]; if (val.empty() || !val.isString()) { std::cout << "Error: element Foo in response is empty or no string!" << std::endl; return 1; } if (val.asString() != "bar") { std::cout << "Error: Value of Foo is not \"bar\", but \"" << val.asString() << "\" instead!" << std::endl; return 1; } //check for "X-Custom-Header-Test: scan-tool test suite" val = headers["X-Custom-Header-Test"]; if (val.empty() || !val.isString()) { std::cout << "Error: element X-Custom-Header-Test in response is empty or no string!" << std::endl; return 1; } if (val.asString() != "scan-tool test suite") { std::cout << "Error: Value of X-Custom-Header-Test is not \"scan-tool test suite\", but \"" << val.asString() << "\" instead!" << std::endl; return 1; } std::cout << "Curly's headers are just fine." << std::endl; return 0; }
int main() { Curly post; //check, if URL is empty if (post.getURL() != "") { std::cout << "Error: URL precondition not met." << std::endl; return 1; } //set URL const std::string HeadToThisPlaceSecurely = "https://httpbin.org/post"; post.setURL(HeadToThisPlaceSecurely); //... and check new value if (post.getURL() != HeadToThisPlaceSecurely) { std::cout << "Error: URL postcondition not met." << std::endl; return 1; } //add some fields post.addPostField("foo", "bar"); post.addPostField("ping", "pong"); //perform post request std::string response = ""; if (!post.perform(response)) { std::cout << "Error: Could not perform post request!" << std::endl; return 1; } //check HTTP status code if (post.getResponseCode() != 200) { std::cout << "Error: HTTP status code is not 200, it is " << post.getResponseCode() << " instead!" << std::endl; return 1; } //check content type if (post.getContentType() != "application/json" && !post.getContentType().empty()) { std::cout << "Error: Content type is not application/json, it is " << post.getContentType() << " instead!" << std::endl; return 1; } std::cout << "Response:" << std::endl << response << std::endl << std::endl; //check response Json::Value root; // will contain the root value after parsing Json::Reader jsonReader; const bool success = jsonReader.parse(response, root, false); if (!success) { std::cerr << "Error: Unable to parse JSON data from response!" << std::endl; return 1; } const Json::Value form = root["form"]; if (form.empty() || !form.isObject()) { std::cout << "Error: form element in response is empty or no object!" << std::endl; return 1; } //check for "foo" Json::Value val = form["foo"]; if (val.empty() || !val.isString()) { std::cout << "Error: element foo in response is empty or no string!" << std::endl; return 1; } if (val.asString() != "bar") { std::cout << "Error: Value of foo is not \"bar\", but \"" << val.asString() << "\" instead!" << std::endl; return 1; } //check for "ping" val = form["ping"]; if (val.empty() || !val.isString()) { std::cout << "Error: element ping in response is empty or no string!" << std::endl; return 1; } if (val.asString() != "pong") { std::cout << "Error: Value of ping is not \"pong\", but \"" << val.asString() << "\" instead!" << std::endl; return 1; } std::cout << "Curly's POST fields are fine." << std::endl; return 0; }