7

I have a running Wt application based on the tutorials all over the web and I was wondering if there is an elegant way of using Wt to add a some Restful API functionality.

I have a few resources I can expose from my current application and I don't want to implement any patches.

If someone has a good idea of how to do that, or even a suggestion of some JSON library that can make the development a breeze, I'd be very thankful.

Roman C
  • 49,761
  • 33
  • 66
  • 176
alexg
  • 902
  • 11
  • 37

1 Answers1

7

You should subclass WResource and implement the WResource::handleRequest method to provide REST API functionality. Then you should add your resource to your server using WServer::addResource

Also you should ensure that you add your resource to the server before the main Wt appliaction entry point:

Wt::WServer server(argv[0]);
server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
server.addResource(new MyResource, "/api"));
server.addEntryPoint(Wt::Application, createApplication);
hank
  • 9,553
  • 3
  • 35
  • 50
  • Why is it important to add the resource before the entry point? – Matt May 25 '15 at 15:44
  • Oh, implementing it like http://redmine.webtoolkit.eu/boards/2/topics/4358 , but with a `WApplication` entry point, then on every path refresh, it tries to re `addResource()`, which throws a fatal error. Implementing it as per this answer for some reason blocked me from reading from my configuration file. – Matt May 25 '15 at 19:18
  • 2
    Hmm, it's too late to edit my comment. Turns out I should be reading config options off the `WServer`, not `WApplication`.. Sorry for spamming this answer with comments :S – Matt May 25 '15 at 20:40