在Flask中获取数据JSON [英] Get Data JSON in Flask

查看:618
本文介绍了在Flask中获取数据JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在这里有很多例子,在那里,我不能让我的API在POST方法工作。
这里是代码:

  from flask import烧瓶,jsonify,请求

@ app.route('/ api / v1 / lists',methods = ['POST'])
def add_entry():$ b $ print(p0)
content = request.get_json ()
appname = content.get('title')
print(content)
print(appname)

当我查询curl(我在Windows上运行它):

curl.exe -i -HContent -type:application / json-X POST -d'{title:titi}' http :// localhost:5000 / api / v1 / lists

curl.exe -i -HContent-Type:application / json-X POST -d{title:读一本书} http :// localhost:5000 / api / v1 / lists



我总是有400个错误回报:

HTTP / 1.0 400 BAD REQUEST
Content-Type:text / html
Content-Leng th:192
服务器:Werkzeug / 0.12.1 Python / 3.6.0
日期:2017年4月4日星期二21:55:29 GMT

错误请求
错误的请求
浏览器(或者代理)发送了一个这个服务器无法理解的请求。

我不知道错误在哪里。



感谢您的帮助。

解决方案

即使使用working代码,如果你的if-block失败(当 value1 value2 None )会导致相同的错误,因为你没有处理if块中的 else 部分。更正应该是:

  @ app.route('/ api / v1 / list',methods = ['POST'] )
def add_entry():
print(p0)
request_json = request.get_json()
value1 = request_json.get('First_Name')
value2 = request_json.get('Last_Name')
response_content = None

如果value1不是None且value2不是None:
print(p3)
cursor .execute(INSERT INTO person(first_name,last_name)VALUES(%s,%s),(value1,value2))
response_content = conn.commit()

return jsonify response_content)

当然,您可能需要比 None 作为回应。


Even following many example here & there, i can't get my API work in POST Method. Here the code about it :

from flask import Flask, jsonify, request

@app.route('/api/v1/lists', methods=['POST'])
def add_entry():
    print("p0")
    content = request.get_json()
    appname = content.get('title')
    print(content)
    print(appname)

When i query with curl (i'm running it on Windows):

curl.exe -i -H "Content-Type: application/json" -X POST -d '{"title":"titi"}' http://localhost:5000/api/v1/lists

curl.exe -i -H "Content-Type: application/json" -X POST -d "{"""title""":"""Read a book"""}" http://localhost:5000/api/v1/lists

I have always a 400 error in return:

HTTP/1.0 400 BAD REQUEST Content-Type: text/html Content-Length: 192 Server: Werkzeug/0.12.1 Python/3.6.0 Date: Tue, 04 Apr 2017 21:55:29 GMT 400 Bad Request Bad Request The browser (or proxy) sent a request that this server could not understand.

I dont see where the error is.

Thanks for your help.

解决方案

Even with the "working" code, in case of your if-block failure (when value1 and value2 is None) will cause the same error, since you are not handling the else part of the if-block. The corrected should be:

@app.route('/api/v1/list', methods=['POST'])
def add_entry():
    print("p0")
    request_json     = request.get_json()
    value1           = request_json.get('First_Name')
    value2           = request_json.get('Last_Name')
    response_content = None

    if value1 is not None and value2 is not None:
        print("p3")
        cursor.execute("INSERT INTO person (first_name,last_name) VALUES (%s,%s)", (value1, value2))
        response_content = conn.commit()

    return jsonify(response_content)

Of course you may want something better than None as the response.

这篇关于在Flask中获取数据JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆