What is producing "405 Method Not Allowed" in this python code (google app engine)?

14,109

Solution 1

The code is correct and works fine. You need to look elsewhere for an explanation of this 405 error.

EDIT

Have you posted your actual code? This code below will give you a 405 Method not Allowed error when you click submit. It has a subtle error in it... :)

import webapp2

form= """
  <html>
  <head>
    <title>Unit 2 Rot 13</title>
  </head>

  <body>
    <h2>Enter some text to ROT13:</h2>
    <form method="post" action="/rot13">
      <textarea name="text"
                style="height: 100px; width: 400px;"></textarea>
      <br>
      <input type="submit">
    </form>
  </body>

  </html> """

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)

class Rot13Handler(webapp2.RequestHandler):
    # Error here: mistyped get instead of post :)
    def get(self):
        text = self.request.get("text")
        self.response.out.write(text)


app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/rot13', Rot13Handler)],
                              debug=True)

And the same would happen if your routing is incorrectly typed, as in:

app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/rot13', MainHandler)],
                              debug=True)

EDIT (Thanks, @Nick Johnson)

If none of the above works, consider starting from scratch and check your GAE set-up.

  1. Do you have a valid app.yaml file alongside the main.py module?
  2. Are you able to run the guestbook demo app in the standard Google AppEngine installation?
  3. If not, post the error messages, if any, as well as the details of the system that you are running it on.
  4. If you are able to run the guestbook, can you try and rebuild your application by editing that one? I have found that this has worked for me in the past.

Solution 2

I am just trying Python following Udacity's online course and met similar issue that the AppEngine unable to find the post method.

And finally it turns out that the root cause is the INDENTION.

I used Notepad++ as the editor for the small project and it just can't work, keep throwing the 405 error. then I copy and paste the code to Netbean IDE with Python plugin installed, the IDE showed that it was wrong indention that made the POST method an inner method of GET method, which could not be found in Notepad++, although it looked like the indention been handled well.

Solution 3

had the same problem. The issue was indentation again. When defining:

def post(self):

if I used 'tab' for indentation it was not working. When I used spaces it did. The error logs showed nothing. To avoid issues like this, you can use a python IDE, like Wing IDE.

Solution 4

I had the same issue with Notepad++. Only thing that I changed with the Python IDLE was to replace some spaces with a tab and it worked fine :)

Share:
14,109
craftApprentice
Author by

craftApprentice

While I'm not fighting with Amazonian anacondas or do not have my notebook stolen by a monkey, I try to develop (usefull, I hope) web applications. Yes, I'm kidding about the animals.

Updated on August 25, 2022

Comments

  • craftApprentice
    craftApprentice over 1 year

    I'm a python newbie and I'm trying to build an app copying step by step what was taught earlier in my class, but I'm getting the "405 Method Not Allowed" error.

    Here what the professor did: enter image description here

    Here what I did:

    enter image description here

    Could someone point me what in the code below is the cause of this error "405 Method Not Allowed"? I can not see difference between what I did and what the professor taught. The indentation is also ok (here is the main.py file https://docs.google.com/open?id=0B8TXLR_e14aCVDFfdlpYSU9DNDg).

    Thanks in advance for any help!

    Here my code:

    form= """
      <html>
      <head>
        <title>Unit 2 Rot 13</title>
      </head>
    
      <body>
        <h2>Enter some text to ROT13:</h2>
        <form method="post" action="/rot13">
          <textarea name="text"
                    style="height: 100px; width: 400px;"></textarea>
          <br>
          <input type="submit">
        </form>
      </body>
    
      </html> """
    
    class MainHandler(webapp2.RequestHandler):
        def get(self):
            self.response.out.write(form)
    
    class Rot13Handler(webapp2.RequestHandler):
        def post(self):
            text = self.request.get("text")
            self.response.out.write(text)
    
    app = webapp2.WSGIApplication([('/', MainHandler), ('/rot13', Rot13Handler)],
                              debug=True)
    
  • craftApprentice
    craftApprentice about 12 years
    Thanks for your attention, @gauden . Yes, it's my actual code, but I'm going to follow your advice and post the results.
  • Nick Johnson
    Nick Johnson about 12 years
    None of your initial suggestions would have anything to do with a 405.
  • daedalus
    daedalus about 12 years
    +1 for the pointer. I still think it is useful to recheck one's system as it helps me in debugging and initial learning of a system, but not because of the specific error message, of course.