XML and JSON Response
Added by Vyacheslav Blinov about 14 years ago
Hi there!
First of all I would like to say thank you for this great framework, I really like it!
Now, back to business, I want to develop webservice with Wt, witch will have a simple site (html frontend), RSS feeds, xml api and json api for various devices and programs witch will use it to gather some info from service.
AFAICS in documentations there are some usefull things to start from, like response, request, but I can hardly understand how to use them to goal my approach, or is there are some other more simple way to do this.
As I understand this, there are some classes or functions that creates "html" code, isn't it? So I only needed to provide same for xml and json and maybe extend WEnviroment?
That I need is:
- provide xml witch is rss feed
- provide xml witch is my custom xml to be parsed by other software
- provide json string to be parsed by other software as server answer
- recieve some GET or POST request parameters to parse them and form a json or xml strings for output to be readed by other software as response
Any help will be appreciated. I'm opened to discussion (actually I tryed to catch someone in irc.freenode.net on #wt but wasn't lucky).
Regards,
Vyacheslav Blinov aka dant3
Replies (6)
RE: XML and JSON Response - Added by Vyacheslav Blinov about 14 years ago
After some hours of reading documentation, I feels like I need to implement some number of subclassed WApplications, one for HTML site, one for RSS, one for XML and one for JSON and use response to generate all non-HTML output with out() stream. Is this a right way to go?
RE: XML and JSON Response - Added by Koen Deforche about 14 years ago
Hey,
Actually, for the non-HTML output, you need to implement resources. If you want these resources to map to 'global' URLs (which I guess is the case for web services), you should deploy them as static resources (WServer::addResource()); if you want to tie them to a session (protected by the session ID), you should add them directly to a WAnchor.
A WApplication only serves a purpose for a normal (HTML/JavaScript/CSS) interactive session.
As an example of an RSS feed which is global, consider the blog example / homepage example, where the RSS feed for the blog is a static resource.
Regards,
koen
RE: XML and JSON Response - Added by Vyacheslav Blinov about 14 years ago
Thank you Koen, I looked at the blog sources, it's just like I was expected - a class using response and a main function (witch I like to keep clean so I moved this code to reimplementation of WRun) witch adds url for it as a resource.
Thanks for hint about WAnchor, this is one of the things I looked for.
But there is one question still opened for me: is there are some classes/functions to handle with xml and json easily in Wt? I couldn't find one.
RE: XML and JSON Response - Added by Koen Deforche about 14 years ago
Hey,
You mean reading or writing XML/JSON ?
Not really. Internally, Wt uses rapidxml for reading (and some times) writing XML.
But you can use whatever library you are used to.
Regards,
koen
RE: XML and JSON Response - Added by Vyacheslav Blinov about 14 years ago
Ok, thanks Koen. I'll try.
RE: XML and JSON Response - Added by Vyacheslav Blinov almost 14 years ago
This solution works great, I'm got good working api with json responses via WResource, however there are some things I miss.
Cause my api behaves like application, I need to get enviroment, and I needed in session handling. Afaiks it is not possible with WResource as is.
To have more details:
My app contains two entry points, one for api (i.e. api.wt) and another for admin-console (i.e. admin.wt). I'm building it with CMake in a way of two separate fcgi`s and as composed wthttp binary with entry point for WApplication and WResource.
I see two possible solutions:
- extend WApplication to have needed response (this looks like more realistic imho)
- extend WResourse to have session support and enviroment
Witch of this solutions I should use?