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 - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -