Reputation: 99
I have a simple restful service that has something strange. The code is below:
from flask import Flask, make_response
from flask_restful import Resource, Api, fields
from get_response_demo import generate_rest_str
import dia_logging
logger = dia_logging.my_logger("api_rest", "demo.log")
app = Flask(__name__)
api = Api(app)
resource_fields = {
'messages': fields.String,
'jsons': fields.String,
}
class RestAPI(Resource):
def get(self, sentence):
dia_result = generate_rest_str(sentence)
logger.info(dia_result)
response = make_response(str(dia_result))
response.mimetype = 'application/json'
return response
api.add_resource(RestAPI, '/<string:sentence>')
if __name__ == '__main__':
app.run(debug=False, use_reloader=False, host='127.0.0.1', port=4000)
And the way I make a request to the service: http://127.0.0.1:4000/sentence=hello
The restful service is hosted on a Ubuntu machine. On my Mac I didn't observed the problem. And the problem is that, whenever I tested a request to the service, in addition to process the real input sent by the parameter 'sentence', and each time it also processes:
INFO:get_response_demo:restful, sentence:favicon.ico
So it processes twice for only one request, and the 'favicon.ico' was processed repeatedly.
How to avoid this? I am new to flask.
Upvotes: 1
Views: 3584
Reputation: 24966
You have at least three options
Add a specific @app.route('/favicon.ico')
before the api.add_resource()
If you're running your API behind nginx or Apache, special-case favicon.ico
in the web server config, so that your Flask code never sees the request
Arrange to serve API calls from /api/...
The latter future-proofs you if you want to add some admin/monitoring capabilities via root-level URLs. You can use #2 and #3 together.
Upvotes: 2