Web Services with Google App Engine

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

Does anyone know how to do this in the Goggle App Engine with Python or, for that matter, in Java (JAS-WX is not supported)? Any links about samples or articles with great thanks.

Thanks//:)

+6
java python google-app-engine web-services
source share
3 answers

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

For example, take a look at a sample guestbook provided by Google that shows an HTTP response returned as / plain text:

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())); } } } 

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

+9
source share

Most python applications just write a handler that directly issues the xml form ... this example caters for any GET requests sent 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() 
+3
source share

It is definitely possible (and not too difficult) to use GAE to host "web services that exchange data via http and receive / return xml."

For the analysis of XML requests (supposedly included in the body of HTTP POST or PUT messages), you have several options, for example. pyexpat or minidom on top, see this thread (especially the last post on it).

If you want, you can also use the mini-module to create an XML response and write back (for example, using StringIO to save the formatted response and its write method as an argument to your minidom instance writexml , then expand and use this instance getvalue to get the desired result as a string). Although you are limited to pure Python and a few “whiteslisted" C-encoded extensions, such as pyexpat, this does not really limit your choice so much, nor does it make your life much more complicated.

Just don’t forget to set the type header of your response to text/xml (or some type of media that is even more specific and appropriate, if any, of course!) - and I recommend using UTF-8 (standard text encoding, which allows you to express all Unicode, being simple ASCII if your data is just ASCII! -), and not strange "code pages" or regionally restricted codes such as the ISO-8859 family.

+2
source share

All Articles