简体   繁体   English

从Python烧瓶API返回Blank

[英]Return Blank from Python flask API

I have an API that must return just blank and status as 200 OK in case there is no data available 我有一个API必须返回空白,状态为200 OK,以防没有可用的数据

I have tried following things and facing these error 我尝试过以下事情并面对这些错误

if df.empty:
   return '' , 200

This returns "" in the browser 这将在浏览器中返回“”

if df.empty:
   return json.loads('{}'), 200

This return {} in the browser 这在浏览器中返回{}

Send status as 204 (NO CONTENT) makes the previous content to be as it is on the browser 发送状态为204(NO CONTENT)使之前的内容与浏览器上的内容一样

How can i return complete blank with status as 200? 如何返回状态为200的完整空白?

I have found solution after thoroughly exploring Flask documents 我在彻底探索Flask文档后找到了解决方案

from flask import Response
.....
if df.empty:
   return Response(status = 200)

Your first example shows in my console, as others have mention in comments: 您的第一个示例显示在我的控制台中,正如其他人在评论中提到的:

127.0.0.1 - - [05/Dec/2018 18:46:35] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [05 / Dec / 2018 18:46:35]“GET / HTTP / 1.1”200 -

You can also see status of a document in a Network tab in Developers console. 您还可以在Developers控制台的“ Network选项卡中查看文档的状态。

I didn't see 204 - NO CONTENT until I explicitly defined it: 在我明确定义它之前,我没有看到204 - NO CONTENT

@app.route('/')
def index():
    return '', 204

Your second example didn't work for me and gave me an error: 你的第二个例子对我不起作用并给了我一个错误:

TypeError: 'dict' object is not callable TypeError:'dict'对象不可调用

EDIT: 编辑:

This is source of a page in opera (and chrome also). 这是opera中的页面源(也是chrome)。

页面来源

And this is code for your second example, that I get an error from. 这是你的第二个例子的代码,我得到了一个错误。

from flask import Flask, json

app = Flask(__name__)


@app.route('/')
def index():
    return json.loads('{}'), 200

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM