How to send as HTTP Post request as JSON in ofx?

Hey!

I need to be able to send JSON in a HTTP Post request by adding a header. I have been able to do it using python, but I’d like to have it integrated in my ofx app. I’ve tried looking into ofxHTTP, but can’t seem to find a working example for setting headers and send JSON.

Do any of you have a working example or some pointers (hehe) lying around?

Best regards

I dont know about ofxHTTP but you can fairly easy do post request with libcurl.

for example: http://stackoverflow.com/a/36991614/542266

thank you, @underdoeg! Never experimented with anything outside ofxAddons, but that was surprisingly easy. Cheers.

well, “regular” c++ programmers also try to solve stuff as easily as possible :wink:

Here’s the ofxHTTP JSON post example. It’s in the develop branch.

https://github.com/bakercp/ofxHTTP/tree/develop/example_basic_client_post_json

sorry I’m just too dumb, how can I get your ofxHTTP from develop branch working? Do I need the newest openframeworks from Github? (ofJson only appears there) And if so, is there a guide somewhere, how to set it up correctly? In my case I tried building ProjectGenerator and the libs with the provided scripts, and I end up having strange errors:

in HTTPClient.h:
line:

std::unique_ptr<BufferedResponse> get(const std::string& uri);

error:

Expected member name or ‘;’ after declaration specifiers
Expected ‘>’

std::string url = "https://test1.io/writejson/";
    
ofx::HTTP::DefaultClient client;
ofx::HTTP::Context context;

ofx::HTTP::BaseResponse response;
ofx::HTTP::PostRequest postRequest(url, Poco::Net::HTTPMessage::HTTP_1_0);
    
//Passing a JSON file
putRequest.setPutFile("file.json");
putRequest.setContentType("text/javascript");
...
// post
std::istream& responseStream = client.execute(postRequest,
                                                      response,
                                                      context);