2

webservice

@app.route('/get-details')
def getDetails():    
   cur.execute("select * from employee")
   rows = cur.fetchall()
   columns = [desc[0] for desc in cur.description]
   result = []
   for row in rows:
           row = dict(zip(columns, row))
           #json_row=json.dumps(row)
           result.append(row)

   json_response=json.dumps(result)
   response=Response(json_response,content_type='application/json; charset=utf-8')
   response.headers.add('content-length',len(json_response))
   response.status_code=200
   return response

webserver

@app.route('/get-details')
def getDetails():
      r=requests.get('http://localhost:8084/get-details')
      return r.json() #error in this line, however r.text is rendering the result but in html

Error

Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 177, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 165, in execute
    application_iter = app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 827, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/test.py", line 855, in run_wsgi_app
    app_iter = app(environ, start_response)
TypeError: 'list' object is not callable

How to handle the response returned from the webservice and treat it like json?

r.text returns ["{\"username\" : \"abhi\", \"pass\" : 2087}"] which is a String object I dont know how r.json will format the result but I want something like :

[{"username" : "abhi", "pass" : 2087}] which I can do using this but I need to have a List not a String to do this.

4
  • 1
    You should be doing r.json() and not r.json? Commented Aug 20, 2014 at 6:40
  • 1
    also, you should really use jsonify in flask to make a json response. It will make your life easier Commented Aug 20, 2014 at 15:43
  • @corvid not a good idea in this case because jsonify only accept top level object. In our case it's a list so it doesn't work. Commented Aug 20, 2014 at 17:41
  • return jsonify({'my_list': my_list}) ;) Commented Aug 20, 2014 at 19:49

1 Answer 1

5

First, use .json() instead of .json.

Then, I think you double json encode your datas.

Try to do that:

for row in rows:
       row = dict(zip(columns, row))
       # REMOVED
       result.append(row)

json_response=json.dumps(result)
response=Response(json_response,content_type='application/json; charset=utf-8')
response.headers.add('content-length',len(json_response))
response.status_code=200
return response

get details

from flask import jsonify

@app.route('/get-details')
def getDetails():
      r=requests.get('http://localhost:8084/get-details')
      json_response=json.dumps(r.json())
      response=Response(json_response,content_type='application/json; charset=utf-8')
      response.headers.add('content-length',len(json_response))
      response.status_code=200
      return response
Sign up to request clarification or add additional context in comments.

6 Comments

but then I get list object is not callable error because of r.json()
I have edited my answer since its a prod code I can not pastebin the whole thing. But I have added the error trace and changes I have made to the code.
@user3089927 it's because you try to "render" a list by doing a return r.json(). You have to jsonify it like Corvid said. I'll update my example.
@Dragu- ValueError: dictionary update sequence element #0 has length 7; 2 is required, I tweaked the output before but the original output is ["{\"username\": \"abhi\", \"uuid\": \"abcd\", \"hostname\": \"XYZ\", \"platform\": \"Windows\", \"cost\": null, \"last_seen\": \"2014-08-18 19:40:00\", \"pass\": 2087}"]. I though it would have no effect.
@user3089927 check now my new edit. It's tested and working. jsonify only accept top level object but it's not your case. So simply use json.dumps() in both case and it will be working :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.