Search code examples
google-app-enginewebapp2

GAE Python WebApp2 url/resource/:id/resource


I am trying to write a API that has a "boat" resource and I want to build off of that url with an ID and another resource.

localhost:8080/boat/absk12712480/arrival

I originally had:

app = webapp2.WSGIApplication([
    ...
    ('/boat/(.*)/arrival', ArrivalHandler),
    ...
    ], debug=True)

But that kept grabbing the "/arrival" as part of the ID. So I tried:

('/boat/(.*?(?=\/)/arrival', ArrivalHandler)
('/boat/(.*?(?=\/arrival)/arrival', ArrivalHandler)

Is what I am trying to do possible and if so how?


Solution

  • In ('/boat/(.*)/arrival', ArrivalHandler) the regex is too greedy, and doesn't match the webapp2 URI format. If the ID is always just lower-case ascii and digits, you could use:

    ('/boat/<:[a-z0-9]+>/arrival', ArrivalHandler)

    ought to work, or

    ('/boat/<id:[a-z0-9]+>/arrival', ArrivalHandler)

    if you want a named parameter.