Hello!
I changed your code to work as expected:
1. remove #if macros
2. remove call of Wt::WRun, which used to prevent the program to enter
try-catch.
3. remove RestApplication class (which is not needed for this example)
4. add beingDeleted(); to ~RestGetHello() (as as stated in documentation)
On Tue, May 29, 2012 at 11:02 AM, Plug Gulp <[email protected]> wrote:
> Hi,
>
> Please forgive my lack of knowledge on both web-development and Wt. I
> am an embedded software engineer and new to both Wt and web
> development. I am trying to learn Wt. Since last couple of days I have
> been trying to understand how to implement RESTful API using Wt. But
> with no success.
>
> How do I implement a RESTful service using Wt? Say, I want to
> implement a simple RESTful API:
>
> http://www.example.com/hello
>
> This API should return the data "Hello World!". How do I achieve this using
> Wt?
>
> I read on the mailing list that WResource is used to implement RESTful
> service in Wt. So I wrote the following code to implement the above
> API:
>
> ////////////////////////////////////////
>
> #include <Wt/WServer>
> #include <Wt/WResource>
> #include <Wt/Http/Response>
>
> using namespace Wt;
>
> class RestGetHello : public Wt::WResource
> {
> public:
> virtual ~RestGetHello(){}
>
> protected:
> virtual void handleRequest(const Wt::Http::Request &request,
> Wt::Http::Response &response)
> {
> response.out() << "Hello World!\n";
> }
> };
>
> #if defined(WITH_WAPP)
> class RestApplication : public WApplication
> {
> public:
> RestApplication(const WEnvironment& env)
> : WApplication(env)
> {
> WServer::instance()->addResource(&getHello, "/hello");
> }
>
> private:
> RestGetHello getHello;
> };
>
> WApplication *createApplication(const WEnvironment& env)
> {
> return new RestApplication(env);
> }
> #endif
>
> int main(int argc, char **argv)
> {
> #if defined(WITH_WAPP)
> return Wt::WRun(argc, argv, &createApplication);
> #else
> try {
> WServer server(argv[0]);
>
> server.setServerConfiguration(argc, argv);
>
> RestGetHello getHello;
>
> server.addResource(&getHello, "/hello");
>
> if (server.start()) {
> WServer::waitForShutdown();
> server.stop();
> }
> } catch (WServer::Exception& e) {
> std::cerr << e.what() << std::endl;
> } catch (std::exception &e) {
> std::cerr << "exception: " << e.what() << std::endl;
> }
> #endif
> }
>
> ///////////////////////////////////////
>
> I am using the following command to access this API:
>
> curl http://www.example.com/hello
>
> But all that I get is the content of an HTML page generated by Wt. I
> am attaching with this e-mail the output of the above command. Please
> could you provide example on how to implement RESTful service using
> Wt?
>
> Thanks and regards,
>
> ~Plug
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> witty-interest mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/witty-interest
>
#include <Wt/WServer>
#include <Wt/WResource>
#include <Wt/Http/Response>
using namespace Wt;
class RestGetHello : public Wt::WResource
{
public:
virtual ~RestGetHello(){
beingDeleted();
}
protected:
virtual void handleRequest(const Wt::Http::Request &request,
Wt::Http::Response &response)
{
response.out() << "Hello World!\n";
}
};
int main(int argc, char **argv)
{
try {
WServer server(argv[0]);
server.setServerConfiguration(argc, argv);
RestGetHello getHello;
server.addResource(&getHello, "/hello");
if (server.start()) {
WServer::waitForShutdown();
server.stop();
}
} catch (WServer::Exception& e) {
std::cerr << e.what() << std::endl;
} catch (std::exception &e) {
std::cerr << "exception: " << e.what() << std::endl;
}
}
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest