使用请求对象瓶获得json响应 [英] getting json response using requests object flask
问题描述
webservice
@ app.route('/ get-details')
def getDetails():
cur.execute(select * from employee)
rows = cur.fetchall()
columns = [desc [0] desc in cur.description]
result = [ ]
行中的行:
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
@ app.route('/ get-details')
def getDetails():
r = requests.get('http:// localhost:8084 / get-details')
return r.json()#error在这一行中,但是r.text是呈现结果,但是在html
错误
请求错误:
Traceback(最近一次调用最后一个):
文件/ usr / local /lib/python2.7/dist-packages/werkzeug/serving.py,第177行,在run_wsgi中
execute(self.server.app)
文件/ usr / local / lib / python2。执行
application_iter = app(environ,start_response)
文件/usr/local/lib/python2.7/dist-packages /flask/app.py,第1836行,在__call__
中返回self.wsgi_app(environ,start_response)
文件/usr/local/lib/python2.7/dist-packages/flask/app .py,第1820行,在wsgi_app
response = self.make_response(self.handle_exception(e))
文件/usr/local/lib/python2.7/dist-packages/flask/app .py,第1403行,在handle_exception
reraise(exc_type,exc_value,tb)
文件/usr/local/lib/python2.7/dist-packages/flask/app.py,行1817,在wsgi_app
response = self.full_dispatch_request()
文件/usr/local/lib/python2.7/dist-packages/ flask / app.py,行1478,在full_dispatch_request
response = self.make_response(rv)
文件/usr/local/lib/python2.7/dist-packages/flask/app.py ,第1577行,在make_response
rv = self.response_class.force_type(rv,request.environ)
文件/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers。 py,第827行,in force_type
response = BaseResponse(* _ run_wsgi_app(response,environ))
文件/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py ,第57行,在_run_wsgi_app
返回_run_wsgi_app(* args)
文件/usr/local/lib/python2.7/dist-packages/werkzeug/test.py,第855行,在run_wsgi_app
app_iter = app(environ,start_response)
TypeError:'list'对象不可调用
如何处理从web服务返回的响应并将其视为json?
r.text返回[{\username \:\abhi \,\pass\:2087}]这是一个字符串对象
我不知道如何r.json格式化的结果,但我想要的东西像:
$ b [{username:abhi ,pass:2087}]我可以使用这个,但我需要一个列表不是一个字符串来做到这一点。 c> .json()而不是
.json
。 试试看: >对于行中的行:
row = dict(zip(columns,row))
#REMOVED
result.append(row)
$ b $ json_response = json.dumps (result)
response = Response(json_response,content_type ='application / json; charset = utf-8')
response.headers.add('content-lengt h',len(json_response))
response.status_code = 200
return response
< h2> 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
返回响应
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.
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
这篇关于使用请求对象瓶获得json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!