【问题标题】:Flask restful - Can't see output from json postFlask restful - 看不到 json 帖子的输出
【发布时间】:2019-08-20 05:10:38
【问题描述】:

我正在使用烧瓶创建 api 服务器,它获取 json 数据的发布。 我使用以下this 教程来创建代码: 从烧瓶进口烧瓶 来自烧瓶导入请求

app = Flask(__name__)

@app.route('/postjson', methods = ['POST'])
def postJsonHandler():
    print (request.is_json)
    content = request.get_json()
    print (content)
    return 'JSON posted'

app.run(host='0.0.0.0')

当我跑步时:

curl -X POST http://127.0.0.1:5000/postjson -H "Content-type: application/json" -d '{ "data": { "url": "https://google.com" }}'

我只看到"JSON posted",没有任何打印。为什么我看不到任何数据? 我也尝试使用 POSTMAN,但结果相同。

我也试过指南示例中的json:

{ 
 "device":"TemperatureSensor", 
 "value":"20", 
 "timestamp":"25/01/2017 10:10:05" 
}

也一样。

编辑-作为@TomMP的答案,当我尝试以下代码时:

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route('/producer', methods = ['POST'])
def postJsonHandler():
    print (request.is_json)
    content = request.get_json()
    print (content)
    return request.get_json()
    #return 'JSON posted'

app.run(host='0.0.0.0')

我明白了:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>

当我尝试调试模式时,我得到:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>TypeError: 'dict' object is not callable
The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict. // Werkzeug Debugger</title>
    <link rel="stylesheet" href="?__debugger__=yes&amp;cmd=resource&amp;f=style.css"
        type="text/css">
... (more lines of data)

【问题讨论】:

    标签: python json flask


    【解决方案1】:

    那是因为您只返回文本“JSON 发布”
    所以返回你想要得到的东西
    像json响应:

    return jsonify({'status': 0, 'msg': 'success'})
    

    细节

    from flask import Flask, request, jsonify
    
    app = Flask(__name__)
    
    @app.route('/postjson', methods = ['POST'])
    def postJsonHandler():
        content = request.json
        print(content)
        return jsonify(content)
    
    app.run(host='0.0.0.0')
    

    调用示例:

    requests.post('http://0.0.0.0:5000/postjson', json={'a':'b'}).json()
    

    【讨论】:

    • 但是我想获取帖子的数据,而不仅仅是随机的json。
    • 尝试返回 jsonify(request.json)
    • 我收到“500 内部服务器错误”
    • 嗨,它部分有效。当我 curl -X POST 127.0.0.1:5000/postjson -H "Content-type: application/json" -d '{ "data": { "url": "google.com" }}" 我确实得到了 JSON。但是当我使用邮递员我得到“空”。你知道为什么吗?thnx
    • 如果mimetype指示JSON(application/json,见is_json()),这将包含解析的JSON数据,否则为None。
    【解决方案2】:

    当您使用 curl 访问路由时,它只会显示该路由返回的内容 - 在本例中为 JSON posted。它不会显示介于两者之间的打印语句。你可以试试run flask in debug mode。这应该会打印到您运行此应用程序的控制台。

    编辑:需要明确的是,您仍然不会收到您发送的作为对您的请求的答复的数据,即在 Postman 中。为此,您必须在函数末尾使用 return request.get_json() 返回数据

    【讨论】:

    • 我尝试了“return request.get_json()”,但我得到“

      服务器遇到内部错误,无法完成您的请求。服务器过载或出现错误在应用程序中。

      ”。当我尝试调试模式时,我也看不到数据(正如您在编辑中提到的那样)。
    【解决方案3】:

    当您使用print() 时,它只会将所有内容打印到控制台,因此请检查它以便您运行应用程序以查看打印输出。您从视图返回的内容(“已发布的 JSON”)是作为响应发送回客户端的内容。

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2019-02-10
      • 2014-07-21
      • 2015-08-10
      相关资源
      最近更新 更多