How to make HTTP POST/GET in a c++ metro app? - c++

I have a some web services that my app needs to communicate with by sending json with REST over HTTP POST/GET. The only way I can see to do this is with the iXMLHTTPRequest2 framework but geeze oh man is it uglier than I want to deal with.
Is this the only option I have to look at with c++?
Would this be easier with c#? I know most of the things are the same across the languages, but if c# has a better way to do this, I'd be willing to make the jump.

You should also look into Project Casablanca - it's a fully native C++ library that also is adapted for use with Metro apps (I believe it wraps IXHR2). I've been using it to perform http REST calls and to parse JSON very successfully.

That's the interface you have to use in C++. It is more difficult than in C#. Given that, there is a good sample that will make using that interface a bit more bearable.

Related

Web Services using C++

I am building a server-client application that involves heavy signal processing (e.g. FFT). I have a working application written in C++/Qt, where everything (signal processing and other calculations) is done in client and server just sends raw data. Now I feel it would be easier to implement these features on the server. So, that maintenance becomes easier.
As I am doing signal processing, I think I should stick to C++ for performance. But I am open to new ideas.
Constraints:
I need type checking so javascript is out of discussion.
Scaling includes adding more server and each server will have at the max
10-12 users. So, Hardware cost is important. I cannot use x number of
i7 processors.
No option of using cloud services.
So, right now my question is as follows:
How can I create web services using C++ for Linux server? (Although cross platform is not important, I would appreciate if I can achieve it.)
EDIT [02:09:2015]
Right now, I think the choice is between poco and C++ Rest SDK. I feel I should go for C++ Rest SDK. Mainly because it has only those features that I need. And Also it is supported by microsoft and uses boost internally. So, I feel in future, this might be well integreated with standard.
You could use cross-platform Poco library to implement HTTP server, it is really straightforward with this framework, and they have a lot of examples. You can also use JSON serialization (like rapidjson library) to implement REST service on top of HTTP - this way your Web service will be accesable by most of the modern Web frameworks.
You might want to take a look at the C++ Rest SDK, an open source, cross platform API from Microsoft.
Like #nogard suggested, I also recommend POCO for now. It's the most serious and feature-full solution. Given you mentioned Qt, I suggest you to take a look at Tufão.
EDIT:
I forgot to mention one comparison of mine on the C++ HTTP server frameworks.
If you directly handle HTTP requests, you might loose the functionality what Web Servers does well what it was build to do. I had a similar issue, what I did was wrap up my Qt c++ code inside a PHP extension. In your case you can do the same. Wrap your logic inside what ever technology you are about to use, doesn't matter it's PHP, net , Java or anything else.

Simple HTTP Server lib

What is a good choice for a simple http Server lib? It doesn't need high performance. I rather look for something simple for some REST/JSON communication ("API").
It must be able though to work in a multithreaded environment and must be able to handle large POST request.
Any suggestions? I already tried cpp-netlib but this seems to be much too complicated for such an easy task...
Edit: I am looking for something really light-weight and simple. E.g. like Sinatra in the Ruby world. Poco is for me another example of a too heavy-weight library.
The first one that comes to mind is Poco Library ( http://pocoproject.org/ )
Cross platform, stable, well documented. While the library itself offers more than you probably need you can build and omit the portions you aren't planning on using to reduce bloat.
They have a fully featured Net library that includes several salient classes and utilities.
Here is a pdf of slides from that library, of particular interest is the HTTPServer class:
http://pocoproject.org/slides/200-Network.pdf
Not sure about large POST data, but I've previously used mongoose: https://github.com/cesanta/mongoose/.
If the LGPL license is unwanted there is a MIT fork from when the project was MIT that also add a C++ API https://github.com/bel2125/civetweb
I would encourage you to start with http server samples in boost.asio. They are so simple and easy to understand, that you should be able to easily extend them as needed.
However, if you want to jump onto something more polished than just sample code, I know of 3 http servers in C++ which you may like to try:
"x0 - HTTP Web Server Framework" to me personally this one seems most promising, because it's lightweight and simple
"highpower / xiva" is a simple http server framework for delivering notifications to browsers
"Pion, a project of Atomic Labs" is a part of elaborate framework for handling large amounts of data
Pretty late answer; but hope this helps.
For your interest of a server that can handle REST, here is the easiest HTTP Server library to use (in my opinion): https://github.com/yhirose/cpp-httplib.
For JSON parsing, you may search for another library to use it in conjunction.
Personally, I'd go for Arachnida but that might be because I wrote it.

How to call a Rest Service in C++?

I want to call a rest service written in WCF (which can support both XML and JSON Web Message Formats) from my C++ application.
What is the best solution to achieve this ? I have seen some utilities (gsoap) which create proxy classes for you to be used to call web services.
Can I achieve the same functionality without using any intermediate utility ? As its a rest service and it works using GET/PUT functions which are basic HTTP functions, is there any C++ library/Solution which could be used to invoke these function directly from a c++ application ?
On Linux, you probably could use curl library (and I guess it is ported to Windows). Curl is a library providing HTTP client functionality to a C or C++ program.
Use Casablanca. This should be helpful for people looking for this answer in 2013. CURL is perfectly appropriate but if you're doing C++ in Windows and using MS stuff, Casablanca seems fit.
Hope the following articles might be of help to you
1. Accessing an XML Web Service Using C++
2. SOAP client for C++
I tried gsoap myself but it became difficult to maintain cross platform versions of my app.
Instead i went the HTTP request route.
For cross platform and C++ i found this Call Rest Web Services from C++
If XML serialization with your REST approach is really not needed then curl is perfect to use. However, if you want type-safe XML serialization in C or C++ then it would become cumbersome to use curl, since you will have to use something that runs on top of curl to process XML such as with a DOM parser (slow and not type safe). If you have a WSDL, then I recommend gSOAP 2.8 which provides integrated REST and XML serialization capabilities (and JSON when you need it).

What are options for GWT to C++ communication?

I am looking for GWT to C++ communication solution.
Currently I am trying to figure out how to run WSDL in GWT, but actually, have absolutely no experience in WSDL, and only little in GWT.
So, my question is about feasibility of working with WSDL in GWT (and how?) and other approaches would also be interesting if exist.
I am trying to avoid coding Java on the server and coding JavaScript on client.
GWT Side:
RequestBuilder and com.google.gwt.json.client.JSONObject for quick and really not that dirty marshaling api.
Overlay types require you to know your data configuration at compile time. With JSONObject (and it's JSONValue's), you can treat it like a slightly unwieldy key/value map.
Set your RequestBuilder to POST and serialize your payload by pushing it into a JSONObject and calling toJSON();
C++ side..
Find a favorite JSON library (may I suggest from the fine choices at http://www.json.org/)
(You'll have to build a method dispatching scheme, but if your app is simple, just use some simple if ()'s)
Send back a response with mime-type of text/javascript;charset=UTF-8.
Back in your GWT code, you read back the results using something like so:
if (Response.SC_OK == response.getStatusCode()) {
try {
String txtResponse = response.getText();
if (txtResponse != null && txtResponse.length() > 0) {
JSONObject result = (JSONObject)JSONParser.parse(testResponse);
//Do something useful...
}
} catch (......)
Now you can talk back and forth with no magic. (And goodness know, no WDSL!!!)
Thrift may be the best solution for you. A specific portage for GWT is available at : http://code.google.com/p/thrift-gwt/ .
You can expose a JSON-based API on your server and use GWT's RequestBuilder and JavaScript Overlay Types to consume it in the client.
You could also use an XML-based API, but JSON will be easiest because of overlay types.
I found Thrift is almost the only choice that includes code-generation-based data-binding solution with RPC support that works on C++ side (original Apache Thrift compiler) and GWT side (gwt-rpc-plus solution).
That is a coincidence, but Thrift is actually a good JSON data-binding solution.
The only problem I see with Thrift (and it's rather inconvenience) - it doesn't support structure polymorphism, which is Ok for JavaScript (Thrift supports it), but bad for real object-oriented languages like C++ and Java.

C++ Library for implementing a web services api over legacy code?

Does anyone know of any really good C++ Libraries for implementing a web services api over top of existing legacy code?
I've got two portions that are in need of it:
An old-school client/server api (No, not web based, that's the problem)
An old cgi application that it integrates with the client and server.
Let me know if you've had any luck in the past implementing something like this using the library.
Microsoft has put out native code webservices API (WWSAPI) that looks pretty decent. I haven't had a chance to use it yet. We had originally ignored it, since it required Windows 7 or Server 2008, but they've finally released a runtime library for older OSs.
I would advise staying away from Microsoft's old SOAP SDK. For one, it's been deprecated; two, it's not terribly easy to distribute; and three, it's terrible to code for compared to the .NET offerings.
What we've done is written a bit of C++\CLI to interface our existing C++ codebase with .NETs webservice framework. This turned out to be remarkably easy. .NET will generate all the classes and boilerplate code you need based of of a WSDL file. Then you just write some C++\CLI code to handle the incoming data as managed classes and fill in some managed classes as responses.
You can use the Apache AXIS/C interface to build a web services interface. It has plugins for Apache and IIS (and I think FastCGI), and lets you talk web services to your legacy code.
I used gSOAP in a project and it was quite straightforward. Compared to Axis/C, I found it easier to learn and use. I never used POCO, can't give you an opinion, but it's gaining popularity recently. This is the link for gSOAP
http://www.cs.fsu.edu/~engelen/soap.html