This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

In Flask, responses are represented by Response objects. Most of the contents of Response packets are processed by the server. In general, we are only responsible for returning the main contents. In the previous article, we learned that Flask would first match the route of the requested URL and call the corresponding view function, whose return value constitutes the main content of the response message.

Flask creates a Response object if the view function returns only one element, Response takes the returned value as the body content, the status code is 200 by default, the MIME type is text/ HTML, and returns the Response object.

A view function can return a tuple of up to one element: the response body, the status code, and the header field. We can also specify the values of these three elements.

@app.route('/hello1')
def hello1() :
    return 'Hello 1'  
    Return Response('Hello 1', status=200, mimeType ='text/ HTML ')


@app.route('/hello2')
def hello2() :
    return 'Hello 2'.201, {'my-headers': 'tigeriaf'}
Copy the code

The hello2() view function above returns a tuple, and the status code value overwrites the default 200 status code. Headers can be a list or dictionary as an additional header.

In fact, we can also build the Response Response object, set some parameters (such as status code, Response, etc.), and then directly return the Response Response object.

@app.route('/hello3')
def hello3() :
    response = make_response('hello 3'.202)
    response.headers["my-headers"] = "tigeriaf"
    return response
Copy the code

The make_response() method is used to build the response object, with the second parameter representing the response status code, which defaults to 200.

In previous cases, we returned plain text, but in real development, we usually return JSON. What should we do?

We all know that json modules in the Python standard library provide json format support for programs, and Flask has done some packaging on the basis of JSON packages. We can directly import Flask JSON packages and return data as JSON types. Change the MIME type of the Response object to applcation/json.

@app.route('/hello4')
def hello4() :
    data = {
        "status": 0."message": "success"."data":
            {"name": "tigeriaf"."age": 24
             }
    }
    json_data = json.dumps(data)
    response = make_response(json_data)
    response.mimetype = "applcation/json"
    return response
Copy the code

Flask also provides more conveniencejsonify()It serializes the data we pass in, converts it to a JSON string as the body of the response, and then generates a response object and automatically sets the MIME type as follows:

@app.route('/hello4')
def hello4() :
    data = {
        "status": 0."message": "success"."data":
            {"name": "tigeriaf"."age": 24}}return jsonify(data)
Copy the code

As you can see, the jsonify() method greatly simplifies our code, and the jsonify() method takes many forms of arguments, either ordinary or keyword arguments, making it very easy to use.

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!