Using ofxHTTP to do AJAX, REST, JSON

Is anyone using ofxHTTP to do AJAX, REST, JSON? I am currently working responding to an external browser client request. Would be interested in any info or code you could provide. At the very least, how you did addroute would be interesting. We do not want to use WebSockets at this time.

1 Like

I’ve created a simple implementation of the Poco library:

/**
 * Setup the http session
 *
 * @param string baseurl
 */
void prHTTP::setup(string base)
{
    uri = base;
    
    session.setHost(uri.getHost());
    session.setPort(uri.getPort());
    session.setKeepAlive(true);
    
    cout << "Started HTTP session with: "+ ofToString(uri.getHost()) +":"+ ofToString(uri.getPort()) << endl;
}


/**
 * Handles the GET requests
 *
 * @param string API path
 * @return string json response
 */
string prHTTP::GET(string path)
{
    string data;
    try {
        if (path.empty()) path = "/";
        
        HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
        session.sendRequest(request);
        
        HTTPResponse response;
        cout << response.getStatus() << " " << response.getReason() << endl;
        
        istream &is = session.receiveResponse(response);
        
        // TODO: check efficientie
        std::istreambuf_iterator<char> eos;
        std::string responseBuff(std::istreambuf_iterator<char>(is), eos);
        data = responseBuff;
    }
    
    catch (Exception &ex) {
        cerr << ex.displayText() << endl;
        return "Error in GET request";
    }
    
    return data;
}


/**
 * Post data to the api
 *
 * @param string path to the api endpoint
 * @param string data the data to send to the api in x-www-form-urlencoded format
 */
void prHTTP::POST(string path, string data)
{
    try {
        HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
        request.setContentType("application/x-www-form-urlencoded");
        request.setKeepAlive(true);
        request.setContentLength(data.length());
        
        string encodedData;
        uri.encode(data, ":", encodedData);
     
        std::ostream& myOStream = session.sendRequest(request);
        myOStream << data;
        
        request.write(std::cout);
        
        HTTPResponse response;
        std::istream& iStream = session.receiveResponse(response);
        std::cerr << iStream.rdbuf();
    }
    
    catch (Exception &ex) {
        cerr << ex.displayText() << endl;
        return "Error in POST request";
    }
}

You can use it like:

// in .h 
prHTTP http;
// in .cpp
http.setup("http://127.0.0.1:5000");
http.POST("/path/to/api/endpoint", "x-www-urlencoded string e.g ?key=var" );

It has a method to POST to a API and to GET from an API. It is probably not as robust as other implementations as it doesn’t have any form of encryption (except when poco applies encryption but i havent checkt). But it works fine with an api i made.

You might check out ofxJSONRPC. It supports JSONRPC via web sockets and POST by default. It is also easy to configure to use SSL, etc. ofSketch uses it WITH websockets. I am using it in production WITHOUT websockets at http://cc.olab.io. I’ll post the server code for http://cc.olab.io someone as soon as I return from my trip. Client code is obviously visible and basically just an extension of the ofxJSONRPC example code.

Thanks, BramdeLeeuw!

Thanks, Chris! Look forward to seeing it!

I duplicated all your files for PostServer and called it RestServer. I then made the changes necessary to support (browser) client side Ajax to RestServer via REST and JSON. The only rather odd issue I have run into is that POCO is stripping the params when using a HTTP POST method…so we are having to use a HTTP GET to recover params on the server side.

I am, of course, referring to your PostServer from ofxHTTP.

1 Like