Home > Backend Development > Python Tutorial > How to use form-data to upload file requests in Python

How to use form-data to upload file requests in Python

王林
Release: 2023-05-15 09:10:05
forward
2535 people have browsed it

Although most of the conventional interfaces are now requested in json form

, it is inevitable that some interfaces need to transmit file streams, and in this case, they need to be uploaded in form-data form

for.e:

The following interface exists. Poster images are created through the interface but thumbnails need to be uploaded.

At this time, the Content-Type of the interface cannot be application/json , but multipart/form-data;

The parameter format is also passed in in the form of a form

It is very simple to set the file flow in postman. Set the file format, and then you can select the file to upload locally. , see the picture below

How to use form-data to upload file requests in Python

The actual request interface can be successful

How to use form-data to upload file requests in Python

Using python to complete the above request requires a clear request method And data type and upload file reading file

import requests
import json
from requests_toolbelt.multipart.encoder import MultipartEncoder
 
url='http://XXX.xxx.xxx.xx:9900/api-marketing-center/poster/save'
 
data={'activityName':'我是个海报2',
        'backgroundPicUrl':'https://xxxxx/default/f865a1aa66adfe50d005bd840e2c3356.jpg',
        'copywriting':'我是个海报  看看我2',
        'endTime':'2021-08-14 16:00:00',
        'startTime':'2021-08-11 16:00:00',
        'giftPicUrl':'https://xxxxx/file/c363c3173ac7c02393bf60018031073d.PNG',
        'qrCodeUrl':'https://xxxxxx/file/c363c3173ac7c02393bf60018031073d.PNG',
        'posterUrlThumbnail':'文件'
        }
files = {"posterUrlThumbnail": open('C:/Users/Lenovo/Desktop/格式测试/12.PNG', "rb")}
 
headers={'AUTH_MERCHANTS_ID': '76',
'Authorization': 'Bearer ad8cbdf3-c827-470a-aba5-076f0f3a9628'}  
 
r=requests.post(url=url,params=data, headers=headers,files=files)
 
print(r.json())
 
#{'code': 0, 'msg': '操作成功', 'data': None}
Copy after login

data: Enter the parameters of the request, among which you need to pay attention to the parameter name of the upload file stream

files = {"posterUrlThumbnail": open( 'C:/Users/Lenovo/Desktop/Format Test/12.PNG', "rb")}

posterUrlThumbnail is the parameter name used for assignment, open the file, and read the assignment

Use the simplest hard-coded headers above,

call the requests library method to request

and you will get the result

Of course, you can also encapsulate a login method in a more complicated way , to call login, obtain token and required header parameters

The above is the detailed content of How to use form-data to upload file requests in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template