[英]How to get valid json resp from Flask app with jsonapi version 1.0, “Content-type”=“application/vnd.api+json”
I have failed to get any resp from the code below. 我无法从下面的代码中得到任何答复。 I have tried different "Content-type", tried building my json in all different ways possible. 我尝试了不同的“ Content-type”,并尝试以所有可能的方式构建json。 Please help. 请帮忙。
@app.route('/api/v1/client', methods={'GET', 'POST'})
def client():
if request=='post':
req = request.get_data()
data = req.json
user_id = data["data"]["attributes"]["user_id"]
redirect_uri = data["data"]["attributes"]["_redirect_uris"]
default_scopes = data["data"]["attributes"]["_default_scopes"]
item = Client(
client_id=gen_salt(40),
client_secret=gen_salt(55),
_redirect_uris=' '.join(redirect_uri),
_default_scopes=' '.join(default_scopes),
user_id=user_id,
allowed_grant_types=' '.join(['implicit', ]),
allowd_response_types=' '.join(['token', ])
)
db.session.add(item)
db.session.commit()
resp = {
"data":{
"attributes":{
"client_id" : "client id",
"client_secret" : "client_secret"
},
"jsonapi":{
"version" : "1.0"
},
"type":"client"
}
}
resp["data"]["attributes"]["client_id"] = item.client_id
resp["data"]["attributes"]["client_secret"]= item.client_secret
status = 200
return (jsonify(resp), status, {"Content-type":"application/vnd.api+json"})
Please, help me! 请帮我! I must be doing something entirely wrong. 我一定在做完全错误的事情。 But a very similar code is working in another function. 但是,非常相似的代码正在另一个函数中工作。 The error which I am getting is "ValueError: View function did not return a response // Werkzeug Debugger". 我得到的错误是“ ValueError:View函数未返回响应// // Werkzeug调试器”。
First of all, if you want to check if the request was sent via POST, you should use if request.method == 'POST':
instead of if request == 'POST':
. 首先,如果要检查请求是否通过POST发送,则应使用if request.method == 'POST':
而不是if request == 'POST':
And if you want to get JSON body of the request, you should use request.get_json()
. 如果要获取请求的JSON正文,则应使用request.get_json()
。
The rest of your work seems fine, but I'd recommend you to read full Flask documentation before doing something. 您的其余工作似乎还不错,但是我建议您在进行操作之前先阅读完整的Flask文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.