0

I have coded a simple WSGI Server using Google app engine. Here's the code for it:

    import webapp2

    class MainHandler(webapp2.RequestHandler):
        def get(self):
            self.response.write('''
                    <html><head><title>BLAH</title></head>
                    <body>
                    <form type = "input" action = "/post" method = post>
                    <input type = text name = "name">
                    <input type = submit value = "Submit">
                    </form>
                    <hr>
                    </body>
                    </html>
                    ''')

    class EntrySubmission(webapp2.RequestHandler):
        def post(self):
            n = self.request.get('name')
            self.response.write('''<html><head><title>%s</title></head>
                        <body>
                        <h1>Welcome %s</h1>
                        <br>How are you today?
                        <br><a href = "/">Back</a>
                        <hr>
                        </body>
                        </html>''' % (n,n))

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

The server works fine when I manually input the value. But when I input the value using python it gives a 405 response. What could be the problem? The code for input via python:

    import requests,json
    url = 'http://localhost:9080/'
    header = {'content-type' : 'application/json'}
    d = {'name' : 'Foo'}
    r = requests.post(url,data = json.dumps(d),headers = header)

And the same problem occurs if I use urrlib2 instead of requests. I even tried using it without sending the headers.

Ishan Garg
  • 178
  • 2
  • 11

1 Answers1

0

You are sending a POST request to / URL, but the handler does implement a post method (hence 405 Method not allowed error). Try to send the request to the correct URL, i.e. /post.

Esenti
  • 707
  • 6
  • 12
  • Ok I did that and it is giving a response 200 but the value I am passing as JSON isn't being passed. So in the output where the name should be it's just blank. I even tried storing the passed value inside a datastore but nothing is getting stored. – Ishan Garg Jul 14 '14 at 19:49
  • The problem is that `request.get()` only works with `application/x-www-form-urlencoded` requests. To get data from JSON request, you need to parse it first — see eg. http://stackoverflow.com/a/19677652/1159088 – Esenti Jul 14 '14 at 20:02