python - Flask-restful: How to only response to requests come with ('Accept':'application/json')? -


i have json api app, working well.now want accept , response json, not response text/html request. app looks this:

class books(resource):     def get(self):         return json.dumps(data) 

someone can help? thanks.

you use preprocessing request handler reject request wrong mimetype. there property of request object (not documented tought, present @ least on flask 0.10) named is_json

given flask app called application, use :

from flask import request, abort, jsonify  @application.before_request def only_json():     if not request.is_json:          abort(400)  # or custom badrequest message 

i use flask jsonify function build reponse, ensure response formatted in json, , sets right headers.

class books(resource):     def get(self):         return jsonify(data) 

Comments

Popular posts from this blog

java - Unable to make sub reports with Jasper -

scala - play framework: Modules were resolved with conflicting cross-version suffixes -

Save and close a word document by giving a name in R -