Skip to content

Instantly share code, notes, and snippets.

@pcannon67
Forked from FatalCatharsis/test.cpp
Created July 27, 2019 20:07
Show Gist options
  • Save pcannon67/d0ebc218a6ca056b9547d58f0c566d4e to your computer and use it in GitHub Desktop.
Save pcannon67/d0ebc218a6ca056b9547d58f0c566d4e to your computer and use it in GitHub Desktop.
A poco http example
Poco::JSON::Object obj;
obj.set("name", "blah");
obj.set("language", "english");
Poco::URI uri("http://the-uri-you-want-to-request-from");
std::string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
HTTPResponse response;
request.setContentType("application/json");
std::stringstream ss;
obj.stringify(ss);
request.setContentLength(ss.str().size());
std::ostream& o = session.sendRequest(request);
obj.stringify(o);
// this line is where you get your response
std::istream& s = session.receiveResponse(response);
Poco::JSON::Parser parser;
Poco::JSON::Object::Ptr ret = parser.parse(s).extract<Poco::JSON::Object::Ptr>();
// (*ret) will contain all the members in a json structure returned
if ((*ret).get("success") != true) {
std::cout << "Failed to upload: " << (*ret).get("message").toString();
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment