Solve the problem of missing data in request.form to obtain list in flask

tags: Python  solution  

When an api interface service is enabled on the flask server, the client often uses request.post to pass data to call the api, as shown below:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests

post_data = {
    "text": ["abcdefg", "defaa", "ghisx"]
}
res = requests.post(url="http://127.0.0.1:5000/analyse", data=post_data)
print(res.text)

The client encapsulates the data whose key is "text" and value is ["abcdefg", "defaa", "ghisx"] and posts it to the server.

The server uses request.form to obtain the post data passed by the client, and then calls get("text") to obtain the value of the key text in the post data, as shown below:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask
from flask import request

app = Flask(__name__)


@app.route('/analyse', methods=['post'])
def analyse():
    data = request.form
    text_list = data.get("text")
    print(text_list, len(text_list))
    return ""


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

However, the output result is only the first item in the passed data, and what is obtained is not the length of the list, but the length of the first string:

abcdefg 7

In fact, when we print out the data, we can find that the data directly sent by the client is in ImmutableMultiDict format.

ImmutableMultiDict([('text', 'abcdefg'), ('text', 'defaa'), ('text', 'ghisx')])

At this point, you need to use the getlist method to read, as shown below:

text_list = data.getlist("text")

The output is:

['abcdefg', 'defaa', 'ghisx'] 3

It can be seen that in this way, the server correctly obtains the data of the client post.

Intelligent Recommendation

HttpWebRequest's request.form does not receive data

Recently written. POST request, use the Postman to simulate, use RAW-JSON, then discover DBCODE does not get parameters. It is found that HTTPWebRequest is only set toapplication/x-www-form-urlencoded...

Android-Firebase Quickly solve the problem of compliance. The second part of the compliance and solve the Firebase_perFormance Library to obtain the software installation list

Articles directory background Strong recommendation preferred reading, there will be relevant knowledge in the future solution Development environment Stack information GetRunningAppProcessses' role S...

Solve the problem of missing precision in java

Solve the problem of missing precision in java In the e-commerce project, the normal float and double data calculations will result in loss of precision, which will have serious consequences when it c...

Solve the problem of QtCreator missing debugger

1. Problem: No compiler engine appears when using QtCreator to compile code 2. Solution: download a windows debugger 1. Download the official website 2. Install winsdksetup 3. Set up QtCreator 4. Debu...

More Recommendation

Solve the missing problem of tsyncflow's nsync_cv.h

I changed the file tensorflow/core/platform/default/mutex.h by adding the relative path as prefix of nsync_cv.h and nsync_mu.h to fix the issue, as follows:...

[] Flask Flask solve the problem of cross-domain

When Flask develop RESTful back-end, front-end cross-domain requests have problems. Here is the solution. Python Version: 3.5.1 downloadflask_corspackage 1 useflask_corsThe CORS, sample code 1 2 3 4 I...

Solve the list loop data coverage problem

When the list loop operation involves modifying the value, the last data will overwrite the existing data. This is caused by the same reference. In this case, you can consider using object copy (with ...

Mybatis solve many, but the problem is only a list of data

Today encounter a problem, record it, on mybatis return data should be generated when an object, each record corresponds to an object, which resulted, object property list only one object. That is mor...

A circular list of data structures to solve the Joseph problem

Joseph question: There are 10 children forming a circle in the order of 1, 2,,,,, 10 clockwise. Starting from No. 1, clockwise 1, 2,,,,, 9 to report the number, whoever reported 9 will be listed (obvi...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top