librelist archives

« back to archive

Getting a blueprint to respond to its URL prefix without the trailing slash

Getting a blueprint to respond to its URL prefix without the trailing slash

From:
Emanuil Tolev
Date:
2013-10-13 @ 14:32
Hi all,

I have a Blueprint called lookup. It defines a single function, api_lookup,
which responds to several routes:

/lookup/
/lookup.json
/lookup/identifier I want to look up, possibly with forward /-es in it

But I don't know of a good way to let it respond to /lookup without a
trailing slash.

This is the corresponding code:

blueprint = Blueprint('lookup', __name__)

@blueprint.route('/', methods=['GET','POST'])
@blueprint.route(".json", methods=['GET','POST'])
@blueprint.route("/<path:path>", methods=['GET','POST'])
def api_lookup(path='',ids=[]):


It's registered it in my application like this:
app.register_blueprint(lookup, url_prefix='/lookup')

How do I define a route such, that api_lookup will respond to POST
"/lookup" *without the trailing slash*?

Right now I've achieved this by importing the function directly from the
blueprint and have defined an additional route (before the blueprint is
imported):

from myapp.view.lookup import api_lookup
# ...

# has to come before the lookup blueprint is registered
# gets disregarded otherwise
@app.route('/lookup', methods=['POST'])
def lookup_without_trailing_slash():
    return api_lookup()

app.register_blueprint(lookup, url_prefix='/lookup')

This seems clunky though, I'd really rather define the route in the
blueprint itself.

Of course I could change the API docs and insist users of my API use the
trailing slash. I'm just asking to see if this is possible right now in
Flask, since I searched quite a bit of stackoverflow, this list and Google
but could not find a clean way to achieve this.

Thanks,
Emanuil

Re: [flask] Getting a blueprint to respond to its URL prefix without the trailing slash

From:
Marius Räsener
Date:
2013-10-13 @ 14:42
Hi Emanuil,

I don't know if thats a good solution but have you tried to register the 
blueprint without the url_prefix?
Than you could use the standard behaviour from flask and add the route 
without the trailing slash. But of course you would have to prefix lookup 
by hand to every route which isn't really DRY. 

Greetings,
Marius

> Am 13.10.2013 um 16:34 schrieb "Emanuil Tolev" <emanuil@cottagelabs.com>:
> 
> Hi all,
> 
> I have a Blueprint called lookup. It defines a single function, 
api_lookup, which responds to several routes:
> 
> /lookup/
> /lookup.json
> /lookup/identifier I want to look up, possibly with forward /-es in it
> 
> But I don't know of a good way to let it respond to /lookup without a 
trailing slash.
> 
> This is the corresponding code:
> 
> blueprint = Blueprint('lookup', __name__)
> 
> @blueprint.route('/', methods=['GET','POST'])
> @blueprint.route(".json", methods=['GET','POST'])
> @blueprint.route("/<path:path>", methods=['GET','POST'])
> def api_lookup(path='',ids=[]):
> 
> 
> It's registered it in my application like this:
> app.register_blueprint(lookup, url_prefix='/lookup')
> 
> How do I define a route such, that api_lookup will respond to POST 
"/lookup" *without the trailing slash*?
> 
> Right now I've achieved this by importing the function directly from the
blueprint and have defined an additional route (before the blueprint is 
imported):
> 
> from myapp.view.lookup import api_lookup
> # ...
> 
> # has to come before the lookup blueprint is registered
> # gets disregarded otherwise
> @app.route('/lookup', methods=['POST'])
> def lookup_without_trailing_slash():
>     return api_lookup()
> 
> app.register_blueprint(lookup, url_prefix='/lookup')
> 
> This seems clunky though, I'd really rather define the route in the 
blueprint itself.
> 
> Of course I could change the API docs and insist users of my API use the
trailing slash. I'm just asking to see if this is possible right now in 
Flask, since I searched quite a bit of stackoverflow, this list and Google
but could not find a clean way to achieve this.
> 
> Thanks,
> Emanuil

Re: [flask] Getting a blueprint to respond to its URL prefix without the trailing slash

From:
Emanuil Tolev
Date:
2013-10-13 @ 14:54
Er, oops. I didn't think of not using a URL prefix. Somebody else
structured the code that way and I hadn't heard of Flask Blueprints before
I read that code. Even though I read the docs I was focused on modifying
the existing code a little too much I guess!

Defining



On 13 October 2013 15:42, Marius Räsener <mr@relatron.de> wrote:

> Hi Emanuil,
>
> I don't know if thats a good solution but have you tried to register the
> blueprint without the url_prefix?
> Than you could use the standard behaviour from flask and add the route
> without the trailing slash. But of course you would have to prefix lookup
> by hand to every route which isn't really DRY.
>
> Greetings,
> Marius
>
> > Am 13.10.2013 um 16:34 schrieb "Emanuil Tolev" <emanuil@cottagelabs.com
> >:
> >
> > Hi all,
> >
> > I have a Blueprint called lookup. It defines a single function,
> api_lookup, which responds to several routes:
> >
> > /lookup/
> > /lookup.json
> > /lookup/identifier I want to look up, possibly with forward /-es in it
> >
> > But I don't know of a good way to let it respond to /lookup without a
> trailing slash.
> >
> > This is the corresponding code:
> >
> > blueprint = Blueprint('lookup', __name__)
> >
> > @blueprint.route('/', methods=['GET','POST'])
> > @blueprint.route(".json", methods=['GET','POST'])
> > @blueprint.route("/<path:path>", methods=['GET','POST'])
> > def api_lookup(path='',ids=[]):
> >
> >
> > It's registered it in my application like this:
> > app.register_blueprint(lookup, url_prefix='/lookup')
> >
> > How do I define a route such, that api_lookup will respond to POST
> "/lookup" *without the trailing slash*?
> >
> > Right now I've achieved this by importing the function directly from the
> blueprint and have defined an additional route (before the blueprint is
> imported):
> >
> > from myapp.view.lookup import api_lookup
> > # ...
> >
> > # has to come before the lookup blueprint is registered
> > # gets disregarded otherwise
> > @app.route('/lookup', methods=['POST'])
> > def lookup_without_trailing_slash():
> >     return api_lookup()
> >
> > app.register_blueprint(lookup, url_prefix='/lookup')
> >
> > This seems clunky though, I'd really rather define the route in the
> blueprint itself.
> >
> > Of course I could change the API docs and insist users of my API use the
> trailing slash. I'm just asking to see if this is possible right now in
> Flask, since I searched quite a bit of stackoverflow, this list and Google
> but could not find a clean way to achieve this.
> >
> > Thanks,
> > Emanuil
>

Fwd: [flask] Getting a blueprint to respond to its URL prefix without the trailing slash

From:
Emanuil Tolev
Date:
2013-10-13 @ 15:02
Apologies for the cut-off email, just learned what Ctrl+Enter does in the
GMail web interface.

Defining both in the blueprint:
@blueprint.route('/lookup', methods=['GET','POST'])
@blueprint.route('/lookup/', methods=['GET','POST'])

results in a
"FormDataRoutingRedirect: A request was sent to this URL (
http://localhost:5000/lookup) but a redirect was issued automatically by
the routing system to "http://localhost:5000/lookup/".  The URL was defined
with a trailing slash so Flask will automatically redirect to the URL with
the trailing slash if it was accessed without one.  Make sure to directly
send your POST-request to this URL since we can't make browsers or HTTP
clients redirect with form data reliably or without user interaction."

But that's fair enough as it's just Werkzeug behaviour, which is in line
with other web servers. Ah well.

I guess I just need to stick to /lookup/ after all. It was bugging me
because it isn't a folder, it's the exposed endpoint of a service, and it
always returns a single document. So it's really a single document /
resource and its URL should not end in /.

Thanks,
Emanuil


On 13 October 2013 15:54, Emanuil Tolev <emanuil@cottagelabs.com> wrote:

> Er, oops. I didn't think of not using a URL prefix. Somebody else
> structured the code that way and I hadn't heard of Flask Blueprints before
> I read that code. Even though I read the docs I was focused on modifying
> the existing code a little too much I guess!
>
> Defining
>
>
>
> On 13 October 2013 15:42, Marius Räsener <mr@relatron.de> wrote:
>
>> Hi Emanuil,
>>
>> I don't know if thats a good solution but have you tried to register the
>> blueprint without the url_prefix?
>> Than you could use the standard behaviour from flask and add the route
>> without the trailing slash. But of course you would have to prefix lookup
>> by hand to every route which isn't really DRY.
>>
>> Greetings,
>> Marius
>>
>> > Am 13.10.2013 um 16:34 schrieb "Emanuil Tolev" <emanuil@cottagelabs.com
>> >:
>> >
>> > Hi all,
>> >
>> > I have a Blueprint called lookup. It defines a single function,
>> api_lookup, which responds to several routes:
>> >
>> > /lookup/
>> > /lookup.json
>> > /lookup/identifier I want to look up, possibly with forward /-es in it
>> >
>> > But I don't know of a good way to let it respond to /lookup without a
>> trailing slash.
>> >
>> > This is the corresponding code:
>> >
>> > blueprint = Blueprint('lookup', __name__)
>> >
>> > @blueprint.route('/', methods=['GET','POST'])
>> > @blueprint.route(".json", methods=['GET','POST'])
>> > @blueprint.route("/<path:path>", methods=['GET','POST'])
>> > def api_lookup(path='',ids=[]):
>> >
>> >
>> > It's registered it in my application like this:
>> > app.register_blueprint(lookup, url_prefix='/lookup')
>> >
>> > How do I define a route such, that api_lookup will respond to POST
>> "/lookup" *without the trailing slash*?
>> >
>> > Right now I've achieved this by importing the function directly from
>> the blueprint and have defined an additional route (before the blueprint is
>> imported):
>> >
>> > from myapp.view.lookup import api_lookup
>> > # ...
>> >
>> > # has to come before the lookup blueprint is registered
>> > # gets disregarded otherwise
>> > @app.route('/lookup', methods=['POST'])
>> > def lookup_without_trailing_slash():
>> >     return api_lookup()
>> >
>> > app.register_blueprint(lookup, url_prefix='/lookup')
>> >
>> > This seems clunky though, I'd really rather define the route in the
>> blueprint itself.
>> >
>> > Of course I could change the API docs and insist users of my API use
>> the trailing slash. I'm just asking to see if this is possible right now in
>> Flask, since I searched quite a bit of stackoverflow, this list and Google
>> but could not find a clean way to achieve this.
>> >
>> > Thanks,
>> > Emanuil
>>
>
>