简体   繁体   English

如何从带有jsonapi版本1.0,“ Content-type” =“ application / vnd.api + json”的Flask应用中获取有效的json resp

[英]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.

相关问题 标头值:使 `application/vnd.api+json` 为必填项 - Header value: make `application/vnd.api+json` mandatory 烧瓶中没有POST请求和Content-Type“application / json”的响应 - No response with POST request and Content-Type “application/json” in flask Flask-Restful 错误:请求 Content-Type 不是 'application/json'。"} - Flask-Restful Error: request Content-Type was not 'application/json'."} 在标头中设置Content-Type / application / json - Setting Content-Type / application/json in header 应用程序引擎:在本地运行时摆脱多余的行(状态:200内容类型:application / json;等) - app engine: get rid of extra lines (Status: 200 content-type: application/json; etc) when running locally 无法使用urllib2将content-type设置为application / json - Not possible to set content-type to application/json using urllib2 JsonResponse未使用Django将内容类型设置为application / json - JsonResponse not setting content-type to application/json using django Flask请求和application / json内容类型 - Flask request and application/json content type 如何将请求和响应“Content-Type”设置为“application/json;charset=UTF-8”? - How to set Request and response “Content-Type” to “application/json;charset=UTF-8”? 将Content-Type设置为application / json时,如何防止转义序列? - How to prevent escaping sequence when I set Content-Type to application/json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM