ayruos
July 3, 2018, 8:25am
#1
I’m trying to do a HTTP post request from openFrameworks and can’t seem to figure it out.
The info here doesn’t seem to work:
thank you, @underdoeg ! Never experimented with anything outside ofxAddons, but that was surprisingly easy. Cheers.
@bakercp ’s link is giving an error 404 on GitHub and @moebiussurfing code gives me a bunch of errors, copy pasting them below:
No type named 'DefaultClient' in namespace 'ofx::HTTP'
No type named 'BaseResponse' in namespace 'ofx::HTTP'; did you mean 'Response'?
No member named 'setPutFile' in 'ofx::HTTP::PostRequest'
Use of undeclared identifier 'putRequest'; did you mean 'postRequest'?
I’m being able to generate the JSON file that I need inside oF, save it to disk and now I need to send it as a http post request. I would need to read the http response as well.
Help would be appreciated
hey @ayruos ,
did you checked like this?:
I do not tryed again recently but I was using 0.9.8 with the stable branch…
(It would be nice some good examples with authentication login and maybe a Google Cloud / Firebase example)
//--------------------------------------------------------------
void ofApp::apiUploadPost(){
// https://firebasestorage.googleapis.com/v0/b/xxxxxx.com/o?uploadType=media&name=imagenes/myPictureObject01
//std::string url = "https://prfct-arbo-labs.firebaseapp.com/";
//std::string url = "http://localhost:5000/";
std::string url = "https://firebasestorage.googleapis.com/v0/b/prfct-arbo-labs.appspot.com/o?uploadType=media&name=imagenes/myPictureObject01";
ofx::HTTP::DefaultClient client;
ofx::HTTP::Context context;
ofx::HTTP::BaseResponse response;
ofx::HTTP::PostRequest post(url, Poco::Net::HTTPMessage::HTTP_1_1);
// post.setFormEncoding(ofx::HTTP::PostRequest::FORM_ENCODING_MULTIPART);
// post.setFormEncoding(ofx::HTTP::PostRequest:);
post.set("contentType", "image/png");
post.setContentType("image/png");
post.set
try
{
// Execute the request and get the response stream.
std::istream& responseStream = client.execute(post,
response,
context);
// Request and response headers can be examined here.
// Copy the output to the terminal.
Poco::StreamCopier::copyStream(responseStream, std::cout);
// Flush the input stream.
std::cout << std::endl;
}
catch (const Poco::Exception& exc)
{
ofLogError("ofApp::setup") << "Got Exception " << exc.displayText() << " " << exc.code();
}
catch (...)
{
ofLogError("ofApp::setup") << "Got unknown exception.";
}
}
EDIT: is not what you are asking for… I can’t find the code I used for the API url POST…
I found the code I used to access Firebase server with the API / JSON with some help notes.
I used the github branch with ofxHTTP (main branch), but some time ago:
void ofApp::API_listSeeds(string user){
std::string url;
//-
// 1 - LIST SEEDS FROM USER:
// ROOT:
// url = "https://XXXXX.firebaseio.com/.json?print=pretty";
// SEEDS DE USER:
url = "https://XXXXX.firebaseio.com/seeds.json?orderBy=\"owner\"&equalTo=\""+user+"\"&print=pretty";
//url = "https://XXXXX.firebaseio.com/seeds.json?orderBy=\"owner\"&equalTo=\"manu\"&print=pretty";
//-
// list seeds:
//url = "https://xxxxx.firebaseio.com/seeds.json?print=pretty";
// WITH DOWNLOAD
//url = "https://xxxxx.firebaseio.com/seeds.json?print=pretty&download=seeds.json";
//https://FIREBASE_NAME.firebaseio.com/enumerations/events.json?print=pretty&download=events.json
// download json:
// std::string url = "https://xxxxx.firebaseio.com/seeds.json?download=myfilename.json";
// query:
// std::string url = "https://xxxxx.firebaseio.com/seeds.json?orderBy="branch"&equalTo="0"&print=pretty";
// query:
// std::string url = "https://xxxxx.firebaseio.com/seeds.json?orderBy=\"$key\"&startAt=\"myItemId0\"&endAt=\"myItemId31\"&print=pretty";
//-
ofx::HTTP::Client client;
ofx::HTTP::Context context;
ofx::HTTP::GetRequest getRequest(url, Poco::Net::HTTPMessage::HTTP_1_1);
//-
try
{
auto response = client.execute(context, getRequest);
// Check the response.
if (response->getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
{
// A successful response.
ofLogNotice("ofApp::setup") << "Response success, expecting " << response->estimatedContentLength() << " bytes.";
// Buffer the response, or otherwise consume the stream.
ofBuffer buffer(response->stream());
ofLogNotice("ofApp::setup") << "Content Begin";
std::cout << buffer << std::endl;
ofLogNotice("ofApp::setup") << "Content End";
//-
ofxJSONElement jsonResponse;
jsonResponse.parse(buffer);
// ofxJSONElement jsonResponseArray;
// jsonResponseArray["seeds"] = jsonResponse;
//// jsonResponseArray.parse(["seeds"] = jsonResponse);
//// jsonResponseArray.append(jsonResponse);
// cout << ">> JSON APPEND+: " << ofToString(jsonResponseArray) << endl;
//// cout << ">> JSON APPEND: " << jsonResponseArray["seeds"][0] << endl;
//// cout << ">> JSON APPEND: " << jsonResponseArray["seeds"][1] << endl;
//// cout << ">> JSON APPEND: " << jsonResponseArray["seeds"][2] << endl;
// jsonResponseArray = jsonResponse.getMemberNames();
cout << ">> OBJECT NAMES: " << ofToString(jsonResponse.getMemberNames()) << endl;
cout << ">> OBJECT SIZE: " << jsonResponse.getMemberNames().size() << endl;
cout << endl;
// for (int i = 0; i < jsonNames.size(); i++){
for (int i = 0; i < jsonResponse.getMemberNames().size(); i++){
string UUID = jsonResponse.getMemberNames()[i];
// string UUID = jsonNames[i];
cout << i << " : " << UUID << endl;
cout << jsonResponse[UUID]["name"].asString() << endl;
cout << jsonResponse[UUID]["owner"].asString() << endl;
cout << jsonResponse[UUID]["URLpng"].asString() << endl;
cout << jsonResponse[UUID]["URLxml"].asString() << endl << endl;
}
}
else
{
ofLogError("ofApp::setup") << response->getStatus() << " " << response->getReason();
}
}
catch (const Poco::Exception& exc)
{
ofLogError("ofApp::setup") << exc.displayText();
}
catch (const std::exception& exc)
{
ofLogError("ofApp::setup") << exc.what();
}
}
another way with CURL, better that your method writing the file btw.
Hope it helps in some way… sorry for the ‘spamming’…
// CURL:
// TODO: set time out and check available internet conection!
//std::string curlCommand = "curl -v -X POST -d "; //debug
std::string curlCommand = "curl -X POST -d ";
//-
// DATABASE JSON:
// std::string curlJson = "'{\"name\": \"seedName\",\"owner\": \"myUser\",\"xmlURL\": \"xmlURL\",\"pngURL\": \"pngURL\"}'";
//-
// API ENDPOINT:
std::string curlUrl = " 'https://xxxxx.firebaseio.com/seeds.json'";
std::string cmd = curlCommand + curlJson + curlUrl;
//EXAMPLE API CMD: "curl -v -X POST -d '{\"author\": \"alanisawesome\",\"title\": \"The Turing Machine\"}' 'https://xxxxx.firebaseio.com/seeds.json'";
string reply = ofSystem(cmd);
//std::cout << "==" << endl << reply << endl << "==" << std::endl;
// GET NEW SEED UUID:
//ofxJSONElement response;
ofxJSONElement jsonResponse;
jsonResponse = reply;
//cout << "JSON: " << jsonResponse << endl;
string responseName_UUID = jsonResponse["name"].asString();//UUID field
cout << endl;
cout << ">> CREATED NEW SEED! UUID: '" << responseName_UUID << "'" << endl;