views:

421

answers:

3

I see that Google App Engine can host web applications that will return html, etc. But what about web services that communicate over http and accept / return xml?

Does anyone know how this is being done in Goggle App Engine with Python or for that matter in Java (JAS-WX is not supported)? Any links o samples or articles is greatly appreciated.

Thanks // :)

+4  A: 

Google App Engine allows you to write web services that return any type of HTTP response content. This includes xml, json, text, etc.

For instance, take a look at the guestbook sample project offered by Google which shows the HTTP response coming back as text/plain:

    public class GuestbookServlet extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            if (user != null) {
                resp.setContentType("text/plain");
                resp.getWriter().println("Hello, " + user.getNickname());
            } else {
                resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
            }
        }
   }

Additionally, the app engine google group is a great place to learn more, ask questions, and see sample code.

shek
+2  A: 

Most python apps just write a handler that outputs the shaped xml directly... this example serves any GET requests submitted to the root url ("/"):

import wsgiref.handlers

from google.appengine.ext import webapp

class MainHandler(webapp.RequestHandler):

  def get(self):
    self.response.out.write('<myXml><node id=1 /></myXml>')

def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()
EPiddy
+1  A: 

It's definitely possible (and not too hard) to use GAE to host "web services that communicate over http and accept / return xml".

To parse XML requests (presumably coming in as the body of HTTP POST or PUT requests), you have several options, e.g. pyexpat or minidom on top of it, see this thread for example (especially the last post on it).

If you wish, you could also use minidom to construct the XML response and write it back (e.g. using a StringIO instance to hold the formatted response and its write method as the argument to your minidom instance's writexml method, then turning around and using the getvalue of that instance to get the needed result as a string). Even though you're limited to pure Python and a few "whiteslisted" C-coded extensions such as pyexpat, that doesn't really limit your choices all that much, nor make your life substantially harder.

Just do remember to set your response's content type header to text/xml (or some media type that's even more specific and appropriate, if any, of course!) -- and, I recommend, use UTF-8 (the standard text encoding that lets you express all of Unicode while being plain ASCII if your data do happen to be plain ASCII!-), not weird "code pages" or regionally limited codes such as the ISO-8859 family.

Alex Martelli