compguy24
compguy24

Reputation: 957

Pass JSON response from requests call to Flask response

I have a function that makes a request to an external url and returns the decoded JSON response. I want to add a Flask view to make this call and return the JSON response in my app. The function works in IPython notebook, but not in Flask, I get TypeError: 'dict' object is not callable. How do I return the JSON response from requests through Flask?

def get_one_day(year, month, day):     
    url = 'http://api.wunderground.com/api/' + API_KEY + '/history_' + year + month + day +'/geolookup/q/Beijing/Beijing.json'
    cnt = requests.get(url)
    js = cnt.json()
    return js       

@app.route('/')
def download():    
    response = make_response(get_one_day('2014', '01', '01'))
    return response
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/jm/Dev/weather/weather.py", line 40, in download
response = make_response(get_one_day('2014', '01', '01'))
File "/anaconda/lib/python2.7/site-packages/flask/helpers.py", line 183, in make_response
return current_app.make_response(args)
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line 841, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/anaconda/lib/python2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'dict' object is not callable

Upvotes: 0

Views: 2824

Answers (1)

davidism
davidism

Reputation: 127380

You are not returning a valid response. Flask expects a string, tuple, or Response object, but you are returning the JSON data (a dict) directly. Rather than decoding the JSON, just pass it on as the response.

return app.response_class(cnt.content, mimetype='application/json')

Or use jsonify to convert a dict to a JSON response.

from flask import jsonify

js = cnt.json()
return jsonify(js)

Upvotes: 4

Related Questions