Skip to content

Instantly share code, notes, and snippets.

@darrenmothersele
Created November 22, 2013 09:06
Show Gist options
  • Save darrenmothersele/7597016 to your computer and use it in GitHub Desktop.
Save darrenmothersele/7597016 to your computer and use it in GitHub Desktop.
Sending base64 encoded data via HTTP in C++ using openFrameworks and Poco Net Libraries
// The main openFrameworks include
#include "ofMain.h"
// Poco is included in openFrameworks 0.8.0
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Base64Encoder.h"
#include "Poco/Net/HTMLForm.h"
#include "Poco/Net/StringPartSource.h"
#include "Poco/StreamCopier.h"
#include "Poco/Buffer.h"
// Create a client session and request object for http://example.com/upload.php
Poco::Net::HTTPClientSession s("example.com",80);
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/upload.php");
// Create a new HTMLForm to submit to and add some basic string data
Poco::Net::HTMLForm form;
form.add("token", "APP_TOKEN");
form.add("username", "USERNAME");
form.add("password", "PASSWORD_HASH");
// Take a screenshot of the current openFrameworks app
ofImage screenImg;
screenImg.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR);
screenImg.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
ofBuffer buffer;
ofSaveImage(screenImg.getPixelsRef(), buffer, OF_IMAGE_FORMAT_JPEG, OF_IMAGE_QUALITY_BEST);
// Convert the binary image data to string using base64 encoding
stringstream ss;
Poco::Base64Encoder b64enc(ss);
b64enc << buffer;
// Add the encodeded image data to the form
form.add("image", ss.str());
// Fill out the request object and write to output stream
form.prepareSubmit(request);
ostream& send = s.sendRequest(request);
form.write(send);
// Get and print out the response from the server
Poco::Net::HTTPResponse response;
istream& res = s.receiveResponse(response);
ostringstream stream;
Poco::StreamCopier::copyStream(res, stream);
cout << stream.str() << endl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment