Search code examples
pythongoogle-app-enginewebapp2

Is it possible to use the same method for both get and post with webapp2?


There is very little difference in my get and post methods. One way to do this would be to put the common logic in another function and call that in both the get and post methods. But before I do that I wanted to know If I can actually have one function handle both, that'll be really neat.


Solution

  • This is a good description of when to use GET vs POST. You can use either, of course, but there are situations where you'd want to use one vs the other. You can use the same methods to process them from within the same class if you wanted to like this:

    class MyHandler(webapp2.RequestHandler): 
    
        def function_to_handle_requests(self):
            # code goes here
    
        def get(self): 
            self.function_to_handle_requests
    
        def post(self):
            self.function_to_handle_requests