5

Is there a way to define optional path parameters in the Falcon web framework? Currently, I'm declaring separate routes for modifying the same resource.

app.add_route('/users/', users_api_post)
app.add_route('/users/{id}', users_api_put)

PS: There's a similar question on SO for Flask framework.

1
  • 1
    Can't you just create two routes that would be handled by the same controller? Commented Sep 7, 2016 at 7:51

1 Answer 1

11

It seems that you are using different HTTP methods, so it seems simpler to use differente functions (on_put and on_post).

But if you are using the same HTTP method, this worked for me:

class EventsResource(object):
    def on_get(self, req, resp, app_id, timeanddate=None):
        if timeanddate:
            ...
        else:
            ...

app.add_route('/events/{app_id}', EventsResource())
app.add_route('/events/{app_id}/{timeanddate}', EventsResource())

Hope this helps...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.