Speed up development with full-stack environments for every branch.

Learn More

Parse JSON Data from Request in TurboGears [Python]

Forked from Rapid Prototyping REST API with TurboGears.

98 Runs 612 Views 242 Copies
Saved

Saved

amol 16

amol
published 3 years ago

from wsgiref.simple_server import make_server
from tg import expose, TGController, AppConfig, request
from tg.decorators import before_validate


@before_validate
def with_json_params(remainder, params):
    """Decorator that enables parsing JSON body as method arguments."""
    if request.content_type == 'application/json':
        params.update(request.json_body)


class RootController(TGController):    
    @expose('index.html')
    def index(self):
      return dict() 

    @expose('json')
    @with_json_params
    def post(self, **params):
      print params
      return params


config = AppConfig(minimal=True, root_controller=RootController())
config.renderers = ['genshi', 'json']

DEPLOY_OPTIONS = {}
httpd = make_server('', 80, config.make_wsgi_app(**DEPLOY_OPTIONS))

print "Serving on port 80..."
httpd.serve_forever()
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).