Plug Gulp
2012-05-29 07:02:39 UTC
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
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