How do I use flask.url_for() with flask-restful?

Houman picture Houman · Jun 14, 2014 · Viewed 11.4k times · Source

I have setup Flask restful like this:

api = Api(app, decorators=[csrf_protect.exempt])
api.add_resource(FTRecordsAPI,
                 '/api/v1.0/ftrecords/<string:ios_sync_timestamp>',
                 endpoint="api.ftrecord")

I would like to redirect internally to the endpoint api.ftrecord.

But the moment I try to do this:

base_url = flask.url_for('api.ftrecord')

I get an exception.

  File "/Users/hooman/workspace/F11A/src/lib/werkzeug/routing.py", line 1620, in build
    raise BuildError(endpoint, values, method)
BuildError: ('api.ftrecord', {}, None)

What am I missing please?

Answer

Martijn Pieters picture Martijn Pieters · Jun 15, 2014

You'll need to specify a value for the ios_sync_timestamp part of your URL:

flask.url_for('api.ftrecord', ios_sync_timestamp='some value')

or you could use Api.url_for(), which takes a resource:

api.url_for(FTRecordsAPI, ios_sync_timestamp='some value')