title: Responding with JSON, XML, or Other, based on .{Type} using web.py
body: <p>I am trying to set response type based on the .{Type} at the end of a URL for a RESTful API using web.py.</p>
  
  <p>How can I pass .JSON, .XML, .HTML, .(whatever) to the class "Assignments" or set it as a value somewhere so that ServerResponse can receive it and respond with the proper format?</p>
  
  <p>I tried:</p>
  
  <pre><code>'/assignments(\.[:upper:]+)', 'Assignments'
  </code></pre>
  
  <p>I'm using the following code for my urls:</p>
  
  <pre><code>urls = (
      '/(.*)/', 'redirect',
      '/', 'Homepage',
      '/assignments', 'Assignments'
  )
  </code></pre>
  
  <p>I have a class "Assignments":</p>
  
  <pre><code>class Assignments:
      def GET(self,responseType):
          sentData = web.data()
          query = JSON.decode(sentData,'unicode')
          # \/ Replace With Code \/
          data = query 
          # /\ Replace with Code /\
          return ServerResponse.Send(data,responseType)
  
      def POST(self,responseType):
          sentData = web.data()
          query = JSON.decode(sentData,'unicode')
          # \/ Replace With Code \/
          data = query 
          # /\ Replace with Code /\
          return ServerResponse.Send(data,responseType)
  </code></pre>
  
  <p>And my ServerResponse class:</p>
  
  <pre><code>class ServerResponse:
      @staticmethod
      def Send(data, method):
          return getattr(ServerResponse, method)(data)
  
      @staticmethod
      def JSON(data):
          web.header('Content-Type', 'application/json')
          response = JSON.encode(data)
          return response
  
      @staticmethod
      def XML(data):
          pass
  
      @staticmethod
      def HTML(data):
          web.header('Content-Type', 'text/html')
          response  = "&lt;html&gt;"
          response += "&lt;head&gt;&lt;/head&gt;"
          response += "&lt;body&gt;"
          response += "{"
          for key in data:
              response += "%s:%s,\n" % (str(key), str(data[key]))
          response += "}"
          response += "&lt;/body&gt;"
          response += "&lt;/html&gt;"
          return response
  </code></pre>