bakercp / ofxHTTP

A suite of HTTP tools, including clients and servers for openFrameworks.
MIT License
110 stars 37 forks source link

POST request with application/json body #38

Closed HalfdanJ closed 8 years ago

HalfdanJ commented 8 years ago

Am I just missing it, or does the postRequest object not support post request where i supply the raw body instead of through a form? I see the PUT request has a setPutBuffer that seems to be what I need, but that is not implemented in POST.

bakercp commented 8 years ago

Ha, I'm literally working on that right this moment. I'll ping back here in a second ...

HalfdanJ commented 8 years ago

Convenient 👍 well, learned a lot of POCO on the way :)

bakercp commented 8 years ago

Good. I'm pushing it to develop momentarily ...

bakercp commented 8 years ago

I just build a nice client for Google Cloud Vision (authentication was the hardest part, but I got JSON Web tokens and oauth2 in ofxHTTP now) and a few other services with it, which I'll also push to develop.

bakercp commented 8 years ago

OK, check out the develop branch. You may need to make sure you're using the develop branch of the dependencies. Once you get it, check out the JSON example.

https://github.com/bakercp/ofxHTTP/blob/develop/example_basic_client_json/src/ofApp.cpp

HalfdanJ commented 8 years ago

Heh, Cloud Vision was also the thing I was working on :) Thanks for the push! Ill take a look at it

HalfdanJ commented 8 years ago

I'm using your post example, trying to post JSON to https://vision.googleapis.com/v1/images:annotate?key=<KEY>

isSuccess() returns false, but no error is printed. I beleive maybe its because i miss Content-Type: application/json in the header?

bakercp commented 8 years ago

Yeah, the post example won't work -- you have to use the json example -- which uses an ofxHTTP::JSONRequest, rather than an ofxHTTP::PostRequest underneath.

HalfdanJ commented 8 years ago

Sorry, also what i meant.. I use the code from the link

bakercp commented 8 years ago

Hm ... let me take a look.

HalfdanJ commented 8 years ago

I dont know if its beacuse im missing a commit in a dependency. I couldn't find any develop branch on the other repos.

bakercp commented 8 years ago

Can you ignore the issuccess and see what data is printed back to you? Also, check the response for error codes ... and also make sure you clean your project :)

HalfdanJ commented 8 years ago

Found it , invalid argument.. Will look at it (tomorrow)

bakercp commented 8 years ago

Cool! Keep me posted and maybe I'll have my ofxCloudPlatform public tomorrow ...

bakercp commented 8 years ago

Hey, found your bug and fixed it here: 9a4a90ae0421098f4300da96acd1aa21dd5191e2

HalfdanJ commented 8 years ago

Worked, thanks!

bakercp commented 8 years ago

By the way @HalfdanJ, you can check out a working example here https://github.com/bakercp/ofxCloudPlatform.

moebiussurfing commented 6 years ago

Hey @bakercp I am trying to make a POST with a binary file on the body, like @HalfdanJ says. (like PUT's method has) I want to upload files to Firebase Storage from OF. The working post format I used in POSTMAN is working:

https://stackoverflow.com/questions/46806545/how-to-upload-objects-to-firebase-storage-using-postman-for-testing/46842973#46842973

`

moebiussurfing commented 6 years ago

nice! solved. :) it worked like that:

std::string url = "https://firebasestorage.googleapis.com/v0/b/xxxxxx.appspot.com/o?uploadType=media&name=myFolder/myPictureObjectName";

ofx::HTTP::Client client;
ofx::HTTP::Context context;
ofx::HTTP::PostRequest postRequest(url, Poco::Net::HTTPMessage::HTTP_1_1);

postRequest.setContentType("image/png");
postRequest.addFormFile("", "filename.png");
try
{
auto response = client.execute(context, postRequest);
...
}
moebiussurfing commented 6 years ago

hmm.... sorry @bakercp for still bothering you with this, but I think sometimes it's useful to have some of your PUT methods (setPutFile, setPutBuffer...) on a POST request. Now I am trying to make a POST to the Firebase database to create a new object like this:

Saving Lists of Data

To generate a unique, timestamp-based key for every child added to a Firebase database reference we can send a POST request. For our users path, it made sense to define our own keys since each user has a unique username. But when users add blog posts to the app, we'll use a POST request to auto-generate a key for each blog post:

curl -X POST -d '{ "author": "alanisawesome", "title": "The Turing Machine" }' 'https://docs-examples.firebaseio.com/rest/saving-data/fireblog/posts.json' Our posts path now has the following data:

{ "posts": { "-JSOpn9ZC54A4P4RoqVa": { "author": "alanisawesome", "title": "The Turing Machine" } } } Notice that the key -JSOpn9ZC54A4P4RoqVa was automatically generated for us because we used a POST request. A successful request will be indicated by a 200 OK HTTP status code, and the response will contain the key of the new data that was added:

{"name":"-JSOpn9ZC54A4P4RoqVa"}

I am trying like this, but I am getting a 400 Bad Request (uncomplete message) reply:

std::string url = "https://xxxxxx.firebaseio.com/seeds.json";
ofx::HTTP::Client client;
ofx::HTTP::Context context;
ofx::HTTP::PostRequest postRequest(url, Poco::Net::HTTPMessage::HTTP_1_1);

postRequest.setContentType("text/javascript");
postRequest.addFormFile("", "file.json");
...