Search code examples
pythongoogle-app-enginegethttp-status-code-405

405 Method not Allowed- Get request in Google App Engine, language Python


I know there are a ton of these questions and I have spent hours going through them and trying to figure it out but I am not able to find the problem. My main.py looks like this:

import webapp2
from google.appengine.api import oauth

app = webapp2.WSGIApplication([
    ('/org', 'org.Organization'),
], debug=True)
app.router.add(webapp2.Route(r'/org/<id:[0-9]+><:/?>', 'org.Organization'))
app.router.add(webapp2.Route(r'/org/search', 'org.OrgSearch'))
app.router.add(webapp2.Route(r'/resources', 'resources.Resource'))
app.router.add(webapp2.Route(r'/resources/<rid:[0-9]+>/org/<oid:[0-9]+><:/?>', 'resources.ResourceOrgs'))

And the code for my get and post request looks like this: import webapp2 from google.appengine.ext import ndb import dbase import json

class Organization(webapp2.RequestHandler):
def post(self):
    if 'application/json' not in self.request.accept:
        self.response.status = 406
        self.response.status_message = "API only supports application/json MIME type"
        return
    new_org = dbase.Organization()
    name = self.request.get('name', default_value=None)
    phone = self.request.get('phone', default_value=None)
    street = self.request.get('street', default_value=None)
    city = self.request.get('city', default_value=None)
    state = self.request.get('state', default_value=None)
    if name:
        new_org.name = name
    else:
        self.response.status = 400
        self.response.status_message = "Invalid request, Name is required"
    if phone:
        new_org.phone = phone
    if street:
        new_org.street = street
    if city:
        new_org.city = city
    else:
        self.response.status = 400
        self.response.status_message = "Invalid request, City is required"
    if state:
        new_org.state = state
    else:
        self.response.status = 400
        self.response.status_message = "Invalid request, State is required" 
    key = new_org.put()
    out = new_org.to_dict()
    self.response.write(json.dumps(out))
    return

def get(self, **kwargs):
    if 'application/json' not in self.request.accept:
        self.response.status = 406
        self.response.status_message = "API only supports application/json MIME type"
    if 'id' in kwargs:
        out = ndb.Key(dbase.Organization, int(kwargs['id'])).get().to_dict()
        self.response.write(json.dumps(out))
    else:
        q = dbase.Organization.query()
        keys = q.fetch(keys_only=True)
        results = { 'keys' : [x.id() for x in keys]}
        self.response.write(json.dumps(results))

I hope somebody can help me because I cannot figure it out and I am running out of time. I am using notepad++ but I changed it so it is using spaces instead of tabs.


Solution

  • Try indenting your def get() and def post()