I need to integrate with backend at the moment that requires I make a HTTPS POST request with an Authorization HTTP header, which I have not done before. I am deploying on Windows but developing on OSX. As the built in URL functionality doe not support POST what’s the best way to go about this? From looking at available blocks:
I created a sample that installs with tinderbox and compiles with OSX based on the latter if anyone wants to take a look:
But I am getting some runtime EXC_BAD_ACCESS errors that seem to have something to do with callbacks, perhaps due to porting code from boost asio to standalone asio (changing placeholder and error_code types). The sample seems like it would be straight forward for anyone more familiar with asio so if anyone can offer any advice that would be great!
I’ve been working on an http client with ease of use in mind…https://github.com/ryanbartley/Cinder-HTTP. It has ssl functionality and, hopefully, an easy way to create and set headers. I haven’t worked with post too much but it should be an easy add. I’ll take a look at adding it tomorrow.
I’ve recently used Microsoft’s C++ REST SDK in a project and was impressed by its ease of use. Rather surprisingly for a Microsoft library, it is cross-platform and open-source. It supports SSL out of the box, so you can make HTTPS requests and it uses PPL for pretty cool task-based asynchronous coding.
For the library itself, see:
I’ve created a block that makes it easier to add the SDK to your project, but I have only tested it on Windows and I offer NO SUPPORT whatsoever, so you’re on your own. You can find it here:
Just as a counter point, I have a rather different opinion on REST. It became overwhelming and cumbersome to use it in a recent Mac project as everything has to be wrapped by defines because of the archaic win32 wide char stuff. The above block was written to get away from REST, in fact. It’s been used in production. So, it’s been pretty thoroughly tested.
@paul.houx - thanks for the info but old prejudices make me very apprehensive to incorporate a MS SDK into a cross platform app. It’s great to see MS changing the way they deal with OSS though so I might have to rethink that!
@colin - thanks although I’m reluctant to add a dependancy as large as poco to a project given that cinder already has boost in it, which should cater for the same needs
@ryanbartley - thanks so much for adding that so quickly! I am getting a couple compile errors:
missing std:: from make_shared in responder.hpp
then in requester.hpp I am not sure what to do about:
/Applications/_cinder/Cinder.git/blocks/Cinder-HTTP/src/cinder/http/requester.hpp:30:16: No matching constructor for initialization of 'std::ostream' (aka 'basic_ostream<char>')
Oh no, got a little loose when I pushed it. Surprised that Windows allows that and Mac doesn’t. I’ve seen the problem in Linux, but I had to leave before I could figure out what’s going on. I’ll take a look when I get back tonight. Sorry about that.
I was able to get the Request_Response sample to work on OSX by removing the second argument to the ostream on the line you mentioned. This would then crash in the onHandshake callback in request_response.hpp:115, where the content buffer is nullptr. Wrapping that in an if (content){} should band-aid you for a bit until ryan can sort out a proper fix.
Sorry about all the trouble tonight. I pushed changes to a bunch of the stuff mentioned above and also some changes to the interface that I had wanted to make a while back. They’re now in master. @lithium, thanks for the interim help. @nay, if you had to make a change to the xcode project, feel free to pr. My mac’s hard drive failed yesterday so I wasn’t able to check the changes thoroughly enough. I should be up and running again tomorrow. And feel free to PM me or open issues on the repo with anything you find and I’ll try to be right on it.
Love, love, your Cinder-HTTP block, but a question:
Any advice on constructing a ‘multipart/form-data;’ POST request? I would love to have an easy way to upload multiple images and/or send data points to an api endpoint from cinder, but failing at every turn.
In python, i would do something like:
import requests
files = {}
files['image0'] = open(image0_path, 'rb')
files['image1'] = open(image1_path, 'rb')
data = {}
data['test'] = "This is totally a test."
r = requests.post(endpoint, files=files, data=data)
In Cinder, am faltering on how to construct that part of the request via:
request->appendHeader( http::Content( std::string( "multipart/form-data;" ), data ) );
Looking into it a bit more, it looks as though the form data needs to be separated with a section header that doesn’t exist in any of your form data or files. This link…http://stackoverflow.com/questions/8659808/how-does-http-file-upload-work/8660740...shows what the data should look like. This weekend I can take a stab at trying to encapsulate something similar to the way poco’s wrapped it.
From digging through stackoverflow it looks like ‘noexcept’ is not supported in VS2013, which presumably means this line in error_codes.ipp is the issue?