Get the App
SLTechnology News&Howtos  ›  Development  › 

Python how to request post list

Shulou Source: shulou.com Published: 2022-06-01 18:16:13 10月26日 Update

Editor to share with you how python request post list, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

The method of request post list

Today, with the written service interface, I tried to send some lists and found that the results were not consistent with the actual data, and then began a long journey of searching for information.

Original code

The original code is quite successful in dealing with dictionaries.

Post:url = 'http://127.0.0.1:5000/test'data = {"text":' I had a good time today', "lang": zh} ret = requests.post (url,data=data) get:txt = request.form ['text'] lang = request.form [' lang'] print (txt, lang) print:

I had a great day, zh.

But I want to send a list, such as

Post:url = 'http://127.0.0.1:5000/test'data = {"text":' I had a good time today', "data": [{'Text',' a'}, {'Text',' b'}]} ret = requests.post (url,data=data)

Similar to this situation, the above methods will be invalid, will lose part of the data, the specific reason is not to explore, interested partners can try for themselves.

Solution

1. It is no longer possible to transmit in the same way as before.

two。 We need to transfer it into a string.

3. Here is the core code of post, get

Post: ret = requests.post (url, data=json.dumps ([{'Text':' hello'}, {'Text':' world'}]) get: print (request.data) print: B'[{"Text": "hello"}, {"Text": "world"}]'

Note that the data obtained here is bytecode, which needs to be converted to a string for further processing.

As follows:

Text = bytes.decode (byte_data, encoding='utf-8') request post data has list and dict methods

Encountered python requests post data today

But there is a piece of data like data= {"data1": "[{" code ":" aaa "}]"} requests.post data will prompt an unknown error # extract the contents of the list, use json.dumps to convert list_code = json.dumps ([{"code": "aaa"}]) # and assign a value data= {"data1": list_code} resp = reuqests.post (url,headers=header,data=data)

Just use this method.

The above is all the contents of the article "python how to request post list". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Data method code content article dictionary character string method transmission processing attempt consistency success not much interest reason most bytes actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB OPPO Reno Redmi Shulou Technology