Customize response information. Return Json

A profile,

We all know that when the browser makes a request, the server gives a response. The response contains the returned content type, status code, server version, and so on. The diagram below:

2. Customize response information

If we want to customize the response, we must return a tuple and a data structure such as response,status,headers, or at least response. Status Specifies the HTTP status code, which can be HTTP status code or user-defined status code. Headers refers to an additional response header.

from flask import Flask, abort, Response

app = Flask(__name__)


@app.route('/')
def index(a):
    # return (' custom response information, 502, {" name ":" xiaosong ", "age" : 12})

    # Tuples can be assembled automatically without parentheses
    {"name": "xiaosong", "age": 12} # return 'custom response information ', 502, {"name": "xiaosong", "age": 12}
    
    # Custom status code can add description information
    return 'Custom Response Information'.'520 love error', {"name": "xiaosong"."age": 12}

if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000, debug=True)  # run the program

Copy the code

Let’s debug in the browser:

Third, make_response

This returns custom response information, which you can use with the make_response function. Make_response (), the equivalent of HttpResponse in Django, has the same effect.

from flask import Flask, abort, Response, make_response

app = Flask(__name__)


@app.route('/')
def index(a):
    # return (' custom response information, 502, {" name ":" xiaosong ", "age" : 12})

    # Tuples can be assembled automatically without parentheses
    {"name": "xiaosong", "age": 12} # return 'custom response information ', 502, {"name": "xiaosong", "age": 12}

    # Custom status code can add description information
    # return 'custom response information ', '520 love error', {"name": "xiaosong", "age": 12}
    resp = make_response()
    resp.headers['name'] = 'xiaosong'
    resp.status = '520 love error'
    return resp


if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000, debug=True)  # run the program

Copy the code

Four, jsonify

When we do a back-end separation project, we use ajax requests on the front end. We use JSON as the data format for both front and back interactions. In Django there is a JsonResponse that returns JSON, and in Flask we use Jsonify to return JSON.

from flask import Flask, jsonify

app = Flask(__name__)


@app.route('/')
def index(a):
    data = {
        'name': 'xiaosong'.'age': 12.'gender': 'woman'
    }

    return jsonify(data)


if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000, debug=True)  # run the program

Copy the code

Let’s debug in the browser:

Welcome to follow my official account: