How to make a HTTPS POST request


#1

Hi all,

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:

  • Cinder-Protocol does not seem to currently work with HTTPS.
  • Cinder-Asio ssl branch seems to be missing some of the recent changes to master that enabled it to compiled with ASIO standalone and latest cinder
  • Cinder-Curl is no longer maintained and does not compile

So I thought I’d try to build something bare bones using ASIO. These links seem helpful:

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!

Cheers,
nay.


#2

Hi nay,

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.


#3

Wow, thanks Ryan. That would be amazing - the block looks great!


#4

Hi,

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:

-Paul


Cinder on Linux - the newb frontier... ( moved from old forum )
#5

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.


#6

FYI, I added a post example to the block and made a few changes. Let me know if you end up using it and if you have any problems.


#7

Hello,
I use POCO for HTTP communication :
https://pocoproject.org/
It work fine !


#8

Thanks everyone,

@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>')


#9

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.


#10

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.


#11

Thanks, doing that and fixing a library path in the project settings did the trick!


#12

Hey nay,

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.snepo, 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.

Best,
Ryan


#13

Ah no, sorry to hear about the hard drive! Just made a pull request for that project fix. Thanks again!


#14

@ryanbartley,

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 ) );

Any Advice?

Keith


#15

Hi,

Happy it’s getting some use. So, to be honest I haven’t worked much with the multipart stuff. What error are you seeing? Client or server side?


#16

Hi,
Does this SO post help?


#17

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.


#18

That would be amazing.

I’ve tried a few attempts at building a raw HTTP string that will work, but haven’t had any success getting the form data to show up on the server. I was basing my attempts off https://www.huyng.com/posts/under-the-hood-an-http-request-with-multipartform-data

I’ll keep digging too, but you may have better luck being more familiar with your block.

Thanks!

Keith


#19

~removed my post as I already wrote something similar higher up in this thread.~


#20

@ryanbartley I’m getting a strange error trying to compile an app with Cinder-HTTP master branch on Win10 with VS2013:

Error	11	error C3646: 'noexcept' : unknown override specifier	c:\users\Craig\desktop\cinder_0.9.0_vc2013\cinder_0.9.0_vc2013\blocks\cinder-http\src\cinder\http\error_codes.ipp	26	1	HTTPapp

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?

#define URDL_ERROR_CATEGORY_NOEXCEPT noexcept

have you run into this before?