Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Daniel Fairhead (OMNIvision)
- Date:
- 2014-01-06 @ 20:04
Hi Charles,
You're doing it right, with sending URIs in responses. Don't worry.
The issue is that the flask restful api that you're making doesn't quite
fit into the normal flask routing table, but it has it's own one.
So try:
my_url = api.get_url(HelloWorld)
The flask restful system sits on top of normal flask, and simply makes
it easier to work with things. You could write your 'root' route as
follows if you wanted as well:
from flask import request, jsonify
app.route("/")
def HelloWorld():
if request.method == "GET":
return jsonify({"hello":"world", "url": url_for(HelloWorld)})
Which would do something similar.
Hope this helps,
Dan
On 01/05/2014 04:49 PM, Charles Bueche wrote:
> Dear list,
>
> I'm a beginner with Flask, and try to implement a RESTful web-service to
> get and set config and interfaces of Cisco routers. The project is paid
> and will be open-sourced when done. I'm developing in a virtualenv on OS
> X, but it will run on Ubuntu when done.
>
> I took the basic example from here :
> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>
> and I added a url_for() to add the URL to my JSON answer. The goal is to
> be "HATEOAS". The code becomes :
>
> ------------------------------------------------
>
> from flask import Flask, url_for
> from flask.ext import restful
>
> app = Flask(__name__)
> api = restful.Api(app)
>
> class HelloWorld(restful.Resource):
> def get(self):
> my_url = url_for('get')
> return {'hello': 'world', 'url': my_url}
>
> api.add_resource(HelloWorld, '/')
>
> if __name__ == '__main__':
> app.run(debug=True)
>
> ------------------------------------------------
>
> It fails with this trace:
>
> Traceback (most recent call last):
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
> __call__
> return self.wsgi_app(environ, start_response)
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
> wsgi_app
> response = self.make_response(self.handle_exception(e))
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 250, in error_router
> return self.handle_error(e)
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 268, in handle_error
> raise exc
> TypeError: __init__() takes exactly 4 arguments (2 given)
>
> I would be very happy to get your help to make url_for works. I'm now
> using request.url, but I would like to benefit from the additional
> features of url_for().
>
> TIA,
> Charles
>
>
>
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Cameron White
- Date:
- 2014-01-05 @ 19:02
The problem is the mimetype. If you use jsonify the mimetype will be
handled for you or you can return a Response with the mimetype set to
'application/json'
The following should do what you need.
class HelloWorld(restful.Resource):
def get(self):
my_url = url_for('get')
return Repsonse(
{'hello': 'world', 'url': my_url},
mimetype='application/json',
)
On 01/05/2014 08:49 AM, Charles Bueche wrote:
> Dear list,
>
> I'm a beginner with Flask, and try to implement a RESTful web-service to
> get and set config and interfaces of Cisco routers. The project is paid
> and will be open-sourced when done. I'm developing in a virtualenv on OS
> X, but it will run on Ubuntu when done.
>
> I took the basic example from here :
> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>
> and I added a url_for() to add the URL to my JSON answer. The goal is to
> be "HATEOAS". The code becomes :
>
> ------------------------------------------------
>
> from flask import Flask, url_for
> from flask.ext import restful
>
> app = Flask(__name__)
> api = restful.Api(app)
>
> class HelloWorld(restful.Resource):
> def get(self):
> my_url = url_for('get')
> return {'hello': 'world', 'url': my_url}
>
> api.add_resource(HelloWorld, '/')
>
> if __name__ == '__main__':
> app.run(debug=True)
>
> ------------------------------------------------
>
> It fails with this trace:
>
> Traceback (most recent call last):
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
> __call__
> return self.wsgi_app(environ, start_response)
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
> wsgi_app
> response = self.make_response(self.handle_exception(e))
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 250, in error_router
> return self.handle_error(e)
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 268, in handle_error
> raise exc
> TypeError: __init__() takes exactly 4 arguments (2 given)
>
> I would be very happy to get your help to make url_for works. I'm now
> using request.url, but I would like to benefit from the additional
> features of url_for().
>
> TIA,
> Charles
>
>
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Nic Lorenzen
- Date:
- 2014-01-05 @ 17:22
Charles,
Check out the Flask jsonify module. You'll need to use that since you are
returning a json payload.
On Jan 5, 2014 11:51 AM, "Charles Bueche" <cblists@bueche.ch> wrote:
> Dear list,
>
> I'm a beginner with Flask, and try to implement a RESTful web-service to
> get and set config and interfaces of Cisco routers. The project is paid
> and will be open-sourced when done. I'm developing in a virtualenv on OS
> X, but it will run on Ubuntu when done.
>
> I took the basic example from here :
>
> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>
> and I added a url_for() to add the URL to my JSON answer. The goal is to
> be "HATEOAS". The code becomes :
>
> ------------------------------------------------
>
> from flask import Flask, url_for
> from flask.ext import restful
>
> app = Flask(__name__)
> api = restful.Api(app)
>
> class HelloWorld(restful.Resource):
> def get(self):
> my_url = url_for('get')
> return {'hello': 'world', 'url': my_url}
>
> api.add_resource(HelloWorld, '/')
>
> if __name__ == '__main__':
> app.run(debug=True)
>
> ------------------------------------------------
>
> It fails with this trace:
>
> Traceback (most recent call last):
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
> __call__
> return self.wsgi_app(environ, start_response)
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
> wsgi_app
> response = self.make_response(self.handle_exception(e))
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 250, in error_router
> return self.handle_error(e)
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 268, in handle_error
> raise exc
> TypeError: __init__() takes exactly 4 arguments (2 given)
>
> I would be very happy to get your help to make url_for works. I'm now
> using request.url, but I would like to benefit from the additional
> features of url_for().
>
> TIA,
> Charles
>
>
>
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Charles Bueche
- Date:
- 2014-01-05 @ 20:34
Thanks Nic and Cameron, but I think I was not clear enough. The problem
is not what I do return, the problem is
url_for()
failing with the trace.
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 17:49
> Dear list,
>
> I'm a beginner with Flask, and try to implement a RESTful web-service to
> get and set config and interfaces of Cisco routers. The project is paid
> and will be open-sourced when done. I'm developing in a virtualenv on OS
> X, but it will run on Ubuntu when done.
>
> I took the basic example from here :
> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>
> and I added a url_for() to add the URL to my JSON answer. The goal is to
> be "HATEOAS". The code becomes :
>
> ------------------------------------------------
>
> from flask import Flask, url_for
> from flask.ext import restful
>
> app = Flask(__name__)
> api = restful.Api(app)
>
> class HelloWorld(restful.Resource):
> def get(self):
> my_url = url_for('get')
> return {'hello': 'world', 'url': my_url}
>
> api.add_resource(HelloWorld, '/')
>
> if __name__ == '__main__':
> app.run(debug=True)
>
> ------------------------------------------------
>
> It fails with this trace:
>
> Traceback (most recent call last):
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
> __call__
> return self.wsgi_app(environ, start_response)
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
> wsgi_app
> response = self.make_response(self.handle_exception(e))
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 250, in error_router
> return self.handle_error(e)
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 268, in handle_error
> raise exc
> TypeError: __init__() takes exactly 4 arguments (2 given)
>
> I would be very happy to get your help to make url_for works. I'm now
> using request.url, but I would like to benefit from the additional
> features of url_for().
>
> TIA,
> Charles
>
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 17:49
> Dear list,
>
> I'm a beginner with Flask, and try to implement a RESTful web-service to
> get and set config and interfaces of Cisco routers. The project is paid
> and will be open-sourced when done. I'm developing in a virtualenv on OS
> X, but it will run on Ubuntu when done.
>
> I took the basic example from here :
> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>
> and I added a url_for() to add the URL to my JSON answer. The goal is to
> be "HATEOAS". The code becomes :
>
> ------------------------------------------------
>
> from flask import Flask, url_for
> from flask.ext import restful
>
> app = Flask(__name__)
> api = restful.Api(app)
>
> class HelloWorld(restful.Resource):
> def get(self):
> my_url = url_for('get')
> return {'hello': 'world', 'url': my_url}
>
> api.add_resource(HelloWorld, '/')
>
> if __name__ == '__main__':
> app.run(debug=True)
>
> ------------------------------------------------
>
> It fails with this trace:
>
> Traceback (most recent call last):
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
> __call__
> return self.wsgi_app(environ, start_response)
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
> wsgi_app
> response = self.make_response(self.handle_exception(e))
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 250, in error_router
> return self.handle_error(e)
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 268, in handle_error
> raise exc
> TypeError: __init__() takes exactly 4 arguments (2 given)
>
> I would be very happy to get your help to make url_for works. I'm now
> using request.url, but I would like to benefit from the additional
> features of url_for().
>
> TIA,
> Charles
>
>
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Nic Lorenzen
- Date:
- 2014-01-05 @ 20:52
could not decode message
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Charles Bueche
- Date:
- 2014-01-05 @ 21:21
I need url_for() to get the URL for the method I'm in, so I can provide
it back to the caller. It might seem odd to give back an URL to a
caller, but the goal is to be 100%-RESTfull. See
http://timelessrepo.com/haters-gonna-hateoas and
http://restcookbook.com/Basics/hateoas/ to grab the idea.
My problem is that Flask-restful breaks url_for(). The fact that I
encode it in JSON or not makes no difference.
> Nic Lorenzen <mailto:nlorenzen1@gmail.com>
> 5 January 2014 21:52
>
> In my experience url_for is setup to serve html pages back to the
> client. In your case it appears that you're concerned with serving a
> json response, which url_for is not really intended to do.
>
> Thanks Nic and Cameron, but I think I was not clear enough. The
> problem is not what I do return, the problem is
>
> url_for()
>
> failing with the trace.
>
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 21:34
> Thanks Nic and Cameron, but I think I was not clear enough. The
> problem is not what I do return, the problem is
>
> url_for()
>
> failing with the trace.
>
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 17:49
> Dear list,
>
> I'm a beginner with Flask, and try to implement a RESTful web-service to
> get and set config and interfaces of Cisco routers. The project is paid
> and will be open-sourced when done. I'm developing in a virtualenv on OS
> X, but it will run on Ubuntu when done.
>
> I took the basic example from here :
> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>
> and I added a url_for() to add the URL to my JSON answer. The goal is to
> be "HATEOAS". The code becomes :
>
> ------------------------------------------------
>
> from flask import Flask, url_for
> from flask.ext import restful
>
> app = Flask(__name__)
> api = restful.Api(app)
>
> class HelloWorld(restful.Resource):
> def get(self):
> my_url = url_for('get')
> return {'hello': 'world', 'url': my_url}
>
> api.add_resource(HelloWorld, '/')
>
> if __name__ == '__main__':
> app.run(debug=True)
>
> ------------------------------------------------
>
> It fails with this trace:
>
> Traceback (most recent call last):
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
> __call__
> return self.wsgi_app(environ, start_response)
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
> wsgi_app
> response = self.make_response(self.handle_exception(e))
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 250, in error_router
> return self.handle_error(e)
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 268, in handle_error
> raise exc
> TypeError: __init__() takes exactly 4 arguments (2 given)
>
> I would be very happy to get your help to make url_for works. I'm now
> using request.url, but I would like to benefit from the additional
> features of url_for().
>
> TIA,
> Charles
>
>
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Cameron White
- Date:
- 2014-01-05 @ 21:52
I understand what you are trying to do now and I think I figured out the
solution
https://gist.github.com/cameronbwhite/8274390
1) Use the url_for thats a method of api
2) Give url_for class not the string
On 01/05/2014 01:21 PM, Charles Bueche wrote:
> I need url_for() to get the URL for the method I'm in, so I can
> provide it back to the caller. It might seem odd to give back an URL
> to a caller, but the goal is to be 100%-RESTfull. See
> http://timelessrepo.com/haters-gonna-hateoas and
> http://restcookbook.com/Basics/hateoas/ to grab the idea.
>
> My problem is that Flask-restful breaks url_for(). The fact that I
> encode it in JSON or not makes no difference.
>
>> Nic Lorenzen <mailto:nlorenzen1@gmail.com>
>> 5 January 2014 21:52
>>
>> In my experience url_for is setup to serve html pages back to the
>> client. In your case it appears that you're concerned with serving a
>> json response, which url_for is not really intended to do.
>>
>> Thanks Nic and Cameron, but I think I was not clear enough. The
>> problem is not what I do return, the problem is
>>
>> url_for()
>>
>> failing with the trace.
>>
>> Charles Bueche <mailto:cblists@bueche.ch>
>> 5 January 2014 21:34
>> Thanks Nic and Cameron, but I think I was not clear enough. The
>> problem is not what I do return, the problem is
>>
>> url_for()
>>
>> failing with the trace.
>>
>> Charles Bueche <mailto:cblists@bueche.ch>
>> 5 January 2014 17:49
>> Dear list,
>>
>> I'm a beginner with Flask, and try to implement a RESTful web-service to
>> get and set config and interfaces of Cisco routers. The project is paid
>> and will be open-sourced when done. I'm developing in a virtualenv on OS
>> X, but it will run on Ubuntu when done.
>>
>> I took the basic example from here :
>> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>>
>> and I added a url_for() to add the URL to my JSON answer. The goal is to
>> be "HATEOAS". The code becomes :
>>
>> ------------------------------------------------
>>
>> from flask import Flask, url_for
>> from flask.ext import restful
>>
>> app = Flask(__name__)
>> api = restful.Api(app)
>>
>> class HelloWorld(restful.Resource):
>> def get(self):
>> my_url = url_for('get')
>> return {'hello': 'world', 'url': my_url}
>>
>> api.add_resource(HelloWorld, '/')
>>
>> if __name__ == '__main__':
>> app.run(debug=True)
>>
>> ------------------------------------------------
>>
>> It fails with this trace:
>>
>> Traceback (most recent call last):
>> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
>> __call__
>> return self.wsgi_app(environ, start_response)
>> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
>> wsgi_app
>> response = self.make_response(self.handle_exception(e))
>> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
>> line 250, in error_router
>> return self.handle_error(e)
>> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
>> line 268, in handle_error
>> raise exc
>> TypeError: __init__() takes exactly 4 arguments (2 given)
>>
>> I would be very happy to get your help to make url_for works. I'm now
>> using request.url, but I would like to benefit from the additional
>> features of url_for().
>>
>> TIA,
>> Charles
>>
>>
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Charles Bueche
- Date:
- 2014-01-06 @ 09:50
Cameron you rock ! got it working, thanks a lot !
(these Python objects and classes are somewhat obscure to me. In IT
since a very long time, I still have issues to model these things in my
old brain).
> Cameron White <mailto:cameronbwhite90@gmail.com>
> 5 January 2014 22:52
> I understand what you are trying to do now and I think I figured out
> the solution
>
> https://gist.github.com/cameronbwhite/8274390
>
> 1) Use the url_for thats a method of api
> 2) Give url_for class not the string
>
> On 01/05/2014 01:21 PM, Charles Bueche wrote:
>
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 22:21
> I need url_for() to get the URL for the method I'm in, so I can
> provide it back to the caller. It might seem odd to give back an URL
> to a caller, but the goal is to be 100%-RESTfull. See
> http://timelessrepo.com/haters-gonna-hateoas and
> http://restcookbook.com/Basics/hateoas/ to grab the idea.
>
> My problem is that Flask-restful breaks url_for(). The fact that I
> encode it in JSON or not makes no difference.
>
> Nic Lorenzen <mailto:nlorenzen1@gmail.com>
> 5 January 2014 21:52
>
> In my experience url_for is setup to serve html pages back to the
> client. In your case it appears that you're concerned with serving a
> json response, which url_for is not really intended to do.
>
> Thanks Nic and Cameron, but I think I was not clear enough. The
> problem is not what I do return, the problem is
>
> url_for()
>
> failing with the trace.
>
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 21:34
> Thanks Nic and Cameron, but I think I was not clear enough. The
> problem is not what I do return, the problem is
>
> url_for()
>
> failing with the trace.
>
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 17:49
> Dear list,
>
> I'm a beginner with Flask, and try to implement a RESTful web-service to
> get and set config and interfaces of Cisco routers. The project is paid
> and will be open-sourced when done. I'm developing in a virtualenv on OS
> X, but it will run on Ubuntu when done.
>
> I took the basic example from here :
> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>
> and I added a url_for() to add the URL to my JSON answer. The goal is to
> be "HATEOAS". The code becomes :
>
> ------------------------------------------------
>
> from flask import Flask, url_for
> from flask.ext import restful
>
> app = Flask(__name__)
> api = restful.Api(app)
>
> class HelloWorld(restful.Resource):
> def get(self):
> my_url = url_for('get')
> return {'hello': 'world', 'url': my_url}
>
> api.add_resource(HelloWorld, '/')
>
> if __name__ == '__main__':
> app.run(debug=True)
>
> ------------------------------------------------
>
> It fails with this trace:
>
> Traceback (most recent call last):
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
> __call__
> return self.wsgi_app(environ, start_response)
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
> wsgi_app
> response = self.make_response(self.handle_exception(e))
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 250, in error_router
> return self.handle_error(e)
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 268, in handle_error
> raise exc
> TypeError: __init__() takes exactly 4 arguments (2 given)
>
> I would be very happy to get your help to make url_for works. I'm now
> using request.url, but I would like to benefit from the additional
> features of url_for().
>
> TIA,
> Charles
>
>
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Farhan Ahmed
- Date:
- 2014-01-05 @ 22:26
url_for() is used for generating the url for a given endpoint. It does not
'server pages' or anything like that.
OP: Before you return JSON response, examine the url mapping (url_map())
to see what is mapped and go from there.
I must admit that I don't fully understand the point of returning the url
in the response for which the request was made, because if you didn't know
the URL you won't be able to make the request in the first place...but I
digress and perhaps missing something.
Best,
Farhan
> On Jan 5, 2014, at 2:52 PM, Nic Lorenzen <nlorenzen1@gmail.com> wrote:
>
> In my experience url_for is setup to serve html pages back to the
client. In your case it appears that you're concerned with serving a json
response, which url_for is not really intended to do.
>
> Thanks Nic and Cameron, but I think I was not clear enough. The problem
is not what I do return, the problem is
>
> url_for()
>
> failing with the trace.
>
>> <postbox-contact.jpg> Charles Bueche 5 January 2014 17:49
>> Dear list,
>>
>> I'm a beginner with Flask, and try to implement a RESTful web-service to
>> get and set config and interfaces of Cisco routers. The project is paid
>> and will be open-sourced when done. I'm developing in a virtualenv on OS
>> X, but it will run on Ubuntu when done.
>>
>> I took the basic example from here :
>> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>>
>> and I added a url_for() to add the URL to my JSON answer. The goal is to
>> be "HATEOAS". The code becomes :
>>
>> ------------------------------------------------
>>
>> from flask import Flask, url_for
>> from flask.ext import restful
>>
>> app = Flask(__name__)
>> api = restful.Api(app)
>>
>> class HelloWorld(restful.Resource):
>> def get(self):
>> my_url = url_for('get')
>> return {'hello': 'world', 'url': my_url}
>>
>> api.add_resource(HelloWorld, '/')
>>
>> if __name__ == '__main__':
>> app.run(debug=True)
>>
>> ------------------------------------------------
>>
>> It fails with this trace:
>>
>> Traceback (most recent call last):
>> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
>> __call__
>> return self.wsgi_app(environ, start_response)
>> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
>> wsgi_app
>> response = self.make_response(self.handle_exception(e))
>> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
>> line 250, in error_router
>> return self.handle_error(e)
>> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
>> line 268, in handle_error
>> raise exc
>> TypeError: __init__() takes exactly 4 arguments (2 given)
>>
>> I would be very happy to get your help to make url_for works. I'm now
>> using request.url, but I would like to benefit from the additional
>> features of url_for().
>>
>> TIA,
>> Charles
>>
>> <postbox-contact.jpg> Charles Bueche 5 January 2014 17:49
>> Dear list,
>>
>> I'm a beginner with Flask, and try to implement a RESTful web-service to
>> get and set config and interfaces of Cisco routers. The project is paid
>> and will be open-sourced when done. I'm developing in a virtualenv on OS
>> X, but it will run on Ubuntu when done.
>>
>> I took the basic example from here :
>> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>>
>> and I added a url_for() to add the URL to my JSON answer. The goal is to
>> be "HATEOAS". The code becomes :
>>
>> ------------------------------------------------
>>
>> from flask import Flask, url_for
>> from flask.ext import restful
>>
>> app = Flask(__name__)
>> api = restful.Api(app)
>>
>> class HelloWorld(restful.Resource):
>> def get(self):
>> my_url = url_for('get')
>> return {'hello': 'world', 'url': my_url}
>>
>> api.add_resource(HelloWorld, '/')
>>
>> if __name__ == '__main__':
>> app.run(debug=True)
>>
>> ------------------------------------------------
>>
>> It fails with this trace:
>>
>> Traceback (most recent call last):
>> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
>> __call__
>> return self.wsgi_app(environ, start_response)
>> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
>> wsgi_app
>> response = self.make_response(self.handle_exception(e))
>> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
>> line 250, in error_router
>> return self.handle_error(e)
>> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
>> line 268, in handle_error
>> raise exc
>> TypeError: __init__() takes exactly 4 arguments (2 given)
>>
>> I would be very happy to get your help to make url_for works. I'm now
>> using request.url, but I would like to benefit from the additional
>> features of url_for().
>>
>> TIA,
>> Charles
>>
>>
Re: [flask] cannot get url_for to work with Flask-RESTful
- From:
- Charles Bueche
- Date:
- 2014-01-06 @ 09:29
Hi Farhan,
the goal is to provide the calling URL and some other object URLs based
on this one, eg:
https://gist.github.com/cbueche/8280250
you see at the bottom the URL pointing to the main object, but the more
interesting part are the URLs within the interfaces, allowing a
navigation within the JSON API. So I provide a self-documented REST API.
This is a part of the REST specification, mentioned in HATEOAS documents.
Regs,
Charles
> Farhan Ahmed <mailto:inshany@gmail.com>
> 5 January 2014 23:26
> url_for() is used for generating the url for a given endpoint. It does
> not 'server pages' or anything like that.
>
> OP: Before you return JSON response, examine the url mapping
> (url_map()) to see what is mapped and go from there.
>
> I must admit that I don't fully understand the point of returning the
> url in the response for which the request was made, because if you
> didn't know the URL you won't be able to make the request in the first
> place...but I digress and perhaps missing something.
>
> Best,
> Farhan
>
> On Jan 5, 2014, at 2:52 PM, Nic Lorenzen <nlorenzen1@gmail.com
> <mailto:nlorenzen1@gmail.com>> wrote:
>
> Nic Lorenzen <mailto:nlorenzen1@gmail.com>
> 5 January 2014 21:52
>
> In my experience url_for is setup to serve html pages back to the
> client. In your case it appears that you're concerned with serving a
> json response, which url_for is not really intended to do.
>
> Thanks Nic and Cameron, but I think I was not clear enough. The
> problem is not what I do return, the problem is
>
> url_for()
>
> failing with the trace.
>
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 21:34
> Thanks Nic and Cameron, but I think I was not clear enough. The
> problem is not what I do return, the problem is
>
> url_for()
>
> failing with the trace.
>
> Charles Bueche <mailto:cblists@bueche.ch>
> 5 January 2014 17:49
> Dear list,
>
> I'm a beginner with Flask, and try to implement a RESTful web-service to
> get and set config and interfaces of Cisco routers. The project is paid
> and will be open-sourced when done. I'm developing in a virtualenv on OS
> X, but it will run on Ubuntu when done.
>
> I took the basic example from here :
> http://flask-restful.readthedocs.org/en/latest/quickstart.html#a-minimal-api
>
> and I added a url_for() to add the URL to my JSON answer. The goal is to
> be "HATEOAS". The code becomes :
>
> ------------------------------------------------
>
> from flask import Flask, url_for
> from flask.ext import restful
>
> app = Flask(__name__)
> api = restful.Api(app)
>
> class HelloWorld(restful.Resource):
> def get(self):
> my_url = url_for('get')
> return {'hello': 'world', 'url': my_url}
>
> api.add_resource(HelloWorld, '/')
>
> if __name__ == '__main__':
> app.run(debug=True)
>
> ------------------------------------------------
>
> It fails with this trace:
>
> Traceback (most recent call last):
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1836, in
> __call__
> return self.wsgi_app(environ, start_response)
> File ".../AJ/lib/python2.6/site-packages/flask/app.py", line 1820, in
> wsgi_app
> response = self.make_response(self.handle_exception(e))
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 250, in error_router
> return self.handle_error(e)
> File ".../AJ/lib/python2.6/site-packages/flask_restful/__init__.py",
> line 268, in handle_error
> raise exc
> TypeError: __init__() takes exactly 4 arguments (2 given)
>
> I would be very happy to get your help to make url_for works. I'm now
> using request.url, but I would like to benefit from the additional
> features of url_for().
>
> TIA,
> Charles
>
>