0

Back again here with a flask question. I am a beginner and learn some awesome things from reddit (how to legit code). Here is my code that I have grabbed certain information from an API that I am now trying to host locally through flask.

from flask import Flask, render_template

import httplib

import json

app = Flask(__name__)


@app.route('/')

def index():

    connection = httplib.HTTPConnection('api.football-data.org')

    headers = {'X-Auth-Token': 'this is my api token here', 'X-Response-Control': 'minified'}
    connection.request('GET', '/v1/competitions/426/leagueTable', None, headers)
    response = json.loads(connection.getresponse().read().decode())
    return response


if __name__ == '__main__':
    app.run()

When I run 127.0.0.1:5000 I get:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Here is what my server is telling me!

MacBooks-MBP:Football macbookpro13$ python Footy_Web.py 
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2016-12-20 13:58:17,493] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1642, in full_dispatch_request
    response = self.make_response(rv)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1746, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/Library/Python/2.7/site-packages/werkzeug/wrappers.py", line 847, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/Library/Python/2.7/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "/Library/Python/2.7/site-packages/werkzeug/test.py", line 871, in run_wsgi_app
    app_rv = app(environ, start_response)
TypeError: 'dict' object is not callable
127.0.0.1 - - [20/Dec/2016 13:58:17] "GET / HTTP/1.1" 500 -

I should mention this code works outside of the flask framework!

f_harrison
  • 51
  • 1
  • 8
  • 1
    I am not familiar with flask (but Django instead). My guess is you are trying to send back dict type object `response `. But you need to send HTTP Response. You may google on *how to send dict as response to Flask API* which gave me result: [How to return json using Flask web framework](http://stackoverflow.com/a/13089975/2063361) – Moinuddin Quadri Dec 20 '16 at 20:14

1 Answers1

2

You need to return a valid HTTP response. To do so, you can use jsonify like below:

return jsonify(response)

Do not forget to import jsonify like this:

from flask import jsonify

jsonify() returns a flask.Response() object.

You can also use json.dumps(), but in this case you need to add a http status code and a Content-Type header to return a valid HTTP response:

return json.dumps(response), 200, {'content-type': 'application/json'}
ettanany
  • 19,038
  • 9
  • 47
  • 63