Skip to content

Flask return a list directly #4659

@vba34520

Description

@vba34520
from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return [1, 2, 3]


if __name__ == '__main__':
    app.run()

It will raise TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list.

Then I have to do like this.

from flask import Flask, jsonify

app = Flask(__name__)


@app.route('/')
def index():
    return jsonify([1, 2, 3])


if __name__ == '__main__':
    app.run()

And I found that a dict can be jsonify automatically in app.py#L1924

So why does flask can't jsonify a list like a dict ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions