The type of Form Data sent by the Python post request

Regular Form Data

Most of the Form Data can be submitted directly through normal post requests

import requests

headers = {
    '自己设置的请求头键': '自己设置的请求头键',
    'Content-Type': '网页接受的数据类型'
}

form_data = {
    '对应的键1':'对应的值1',
    '对应的键2':'对应的值2',
}

response = requests.post('需要访问的url地址',data=form_data,headers=headers)
response.close()

Special Form Data

        However, some of the data in Form Data is very cumbersome, and some of them have the following structure

        The decoded data looks more in line with the json format

         But after clicking [view source], I found that it is not conventional data at all. The picture below is what the source data looks like

        Each parameter will start with a fixed format

        For example:

                ------WebKitFormBoundaryxxxxxxx

                Content-Disposition: form-data; name="upload_sign"

         If it is such a parameter, then the method mentioned above cannot be used to send a post request

The solution

Create a form_data object through the [MultipartEncoder] library

from requests_toolbelt import MultipartEncoder

headers = {
    '自己设置的请求头键': '自己设置的请求头键',
}

# 准备需要post的数据
request_data = {
    'upload_sign': signature,
    'forbid_override': 'false',
}

# 这个按照自己需要的来设置
boundary = '----WebKitFormBoundary1hdxbOXZ2CT7I7gW'

form_data = MultipartEncoder(fields=request_data, boundary=boundary)
headers['Content-Type'] = form_data.content_type

response = requests.post('需要访问的url地址',data=form_data,headers=headers)
response.close()

Guess you like

Origin blog.csdn.net/gongzairen/article/details/131946392