addResource
Added by Nezar Ab over 9 years ago
Can anyone guide me through the process of creating a static global resource that serves xml ? I'm trying to create a REST web service but I have no idea where to start. I just keep getting core dumps.
Thanks in advance.
Replies (4)
RE: addResource - Added by Mark Petryk over 9 years ago
Nezar, certainly! It is brain-dead simple. I apologize for the imbedded Qt... I'm a Qt freak, and lean on it too heavily sometimes... I'll let you remove it.
First, create a class as a resource:
921 class MYDBUTIL : public WResource
922 {
923 public:
924 MYDBUTIL( WObject * parent = NULL )
925 : WResource(parent)
926 {
927 dboSession = new XYZ::Session(DBNAME);
928 }
929
930 ~MYDBUTIL()
931 {
933 delete dboSession;
934 }
935
949 std::string getField( const Wt::Http::Request & request, const std::string & fieldName )
950 {
951 std::string retVal;
952
953 const std::string * param = request.getParameter(fieldName);
954 if( param )
955 retVal = *param;
956
957 return retVal;
958 }
959
960 void handleRequest( const Wt::Http::Request & request, Wt::Http::Response& )·
961 {·
962 qDebug() << Q_FUNC_INFO;·
963 qDebug() << QString::fromStdString( request.method() );·
964 debugField( request, "a" );
965
966 std::string action = getField(request,"a");
967
968 if( action \"tableCreationSql\" )
969 qDebug() << QString::fromStdString( dboSession-> tableCreationSql() );
970
971 if( action "dropTables" )
972 {
973 dboSession-> dropTables();
974 qDebug() << "tables dropped";
975 }
976
987
988 }
989
990
991 XYZ::Session * dboSession;
992
993 };
994
Then, in your main() function, set up the resource;
1059 server = new Wt::WServer( argc, argv, WTHTTP_CONFIGURATION );
1060
1061 server-> addResource( new MYSUBMIT(), "/submit" );
1062 server-> addResource( new MYDBUTIL(), "/dbutil" );
1063 server-> addResource( new MYCYFEAPI(), "/cyfeapi" );
1064
Now, when you browse to your site, and access the resource by URL, you will activate your Resource class;
http://your-wt-site.com/dbutil?a=tableCreationSql
If you want to send your response, you could rewrite your handleRequest like this;
1034 void handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response )·
1035 {·
1036 qDebug() << Q_FUNC_INFO;·
1037 qDebug() << QString::fromStdString( request.method() );·
1038 ·
1039 std::string action = getField(request,"q");·
1040 ·
1041 if( action == "1" )·
1042 {·
1043 qDebug() << "sending json response";·
1044 ·
1045 response.out() << "this is a test" << std::endl;·
1046 }·
1047 ·
1048 }·
Hope this helps.
RE: addResource - Added by Wim Dumon over 9 years ago
and examples/blog/BlogRSSFeed is also a fully integrated example of this.
Wim.
RE: addResource - Added by Nezar Ab over 9 years ago
Thank you Mark, Thank you Wim.
I really appreciate your help.
I have also found a very good example on Ed's tech corner : eduardo-lago.blogspot.com/2013/05/creating-rest-web-service-using.html
It would be helpful for anyone who might want to create a web service in Wt.
I really like the idea of being able to implement a service without the need for WApplication !!
Wt Rocks !! :)
RE: addResource - Added by Mark Petryk over 9 years ago
Glad you got it working, Nezar. Thanks for the link as well.