The pinnacle of python requests to complete the case of the interface file upload

This article mainly introduces the case of python requests completing the interface file upload, which has a good reference value and hopes to be helpful to everyone. Come and see Bei Fan together

Recently, I am preparing an open class. The theme is to use different languages ​​and different tools to upload and download files.

When using Jmeter to implement functions, and when using loadrunner to write scripts, everything went smoothly without any problems. When I tried to use Python to solve this problem, it took some time.

This also allows me to find a lot of fun in learning and trying. Let me share with you how to implement the operation.

premise:

1: There is an upload interface, the address is as follows: http://xx.xx.xx.xx//upload/stream (the company's service, the address is inconvenient to send out ~ interested comrades can come to our open class!)

2: The parameters of the upload interface are as follows:

1

{ "parentId":"","fileCategory":"personal","fileSize":179,"fileName":"summer_text_0920.txt","uoType":1}

There are two parameters that need to be explained to you: filesize: refers to the size of the file in bytes. filename: refers to the file name you saved after uploading ~ remember not to get the suffix wrong. Other parameters can be ignored. When making the interface of your own company, you can do it according to the interface document of your own company.

Get started:

1: Install the requests module, the installation command: pip install requests

2: Send a request, the code is as follows:

1

2

3

4

header={ "ct":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"}

files = { 'file':open('D:\\test_data\\summer_test_data_05.txt','rb')}#此处是重点!我们操作文件上传的时候,把目标文件以open打开,然后存储到变量file里面存到一个字典里面

upload_data={ "parentId":"","fileCategory":"personal","fileSize":179,"fileName":"summer_text_0920.txt","uoType":1}

upload_res=requests.post(upload_url,upload_data,files=files,headers=header)##此处是重点!我们操作文件上传的时候,接口请求参数直接存到upload_data变量里面,在请求的时候,直接作为数据传递过去

View Results:

After executing the code, we can see that our upload is successful, and the effect is as follows:

If you have an interface for uploading files, please try it!

Once you learn to code, some things are a lot easier!

Supplement: Two methods for python to implement the interface to upload files

File upload: The type of uploaded image is file, header information is not used here

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import requests

def sendImg(img_path, img_name, img_type='image/jpeg'):

 """

 :param img_path:图片的路径

 :param img_name:图片的名称

 :param img_type:图片的类型,这里写的是image/jpeg,也可以是png/jpg

 """

 url = 'https://www.xxxxxxxxxx.com' # 自己想要请求的接口地址

  

 with open(img_path + img_name, "rb")as f_abs:# 以2进制方式打开图片

 body = {

  # 有些上传图片时可能会有其他字段,比如图片的时间什么的,这个根据自己的需要

   

  'camera_code': (None, "摄像头1"),

   

  'image_face': (img_name, f_abs, img_type)

  # 图片的名称、图片的绝对路径、图片的类型(就是后缀)

   

  "time":(None, "2019-01-01 10:00:00")

   

  }

 # 上传图片的时候,不使用data和json,用files

 response = requests.post(url=url, files=body).json

 return response

  

if __name__=='__main__':

  # 上传图片

  res = sendImg(img_path, img_name)     # 调用sendImg方法

 print(res)

  

**如果上传图片是数组时,value直接写图片路径就可以**

File upload: The upload type is file, and the header information is used

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

# "Content-Type": "multipart/form-data; boundary=76a22e30da2bb7790828887966871012"

from urllib3 import encode_multipart_formdata

import requests

def sendFile(filename, file_path):

 """

 :param filename:文件的名称

 :param file_path:文件的绝对路径

 """

 url = "https://www.xxxxxxx.com" # 请求的接口地址

  with open(file_path, mode="r", encoding="utf8")as f: # 打开文件

 file = {

  "file": (filename, f.read()),# 引号的file是接口的字段,后面的是文件的名称、文件的内容

  "key": "value", # 如果接口中有其他字段也可以加上

   }

  

 encode_data = encode_multipart_formdata(file)

  

 file_data = encode_data[0]

 # b'--c0c46a5929c2ce4c935c9cff85bf11d4\r\nContent-Disposition: form-data; name="file"; filename="1.txt"\r\nContent-Type: text/plain\r\n\r\n...........--c0c46a5929c2ce4c935c9cff85bf11d4--\r\n

  

 headers_from_data = {

   "Content-Type": encode_data[1],

   "Authorization": token

    }

 # token是登陆后给的值,如果你的接口中头部不需要上传字段,就不用写,只要前面的就可以

 # 'Content-Type': 'multipart/form-data; boundary=c0c46a5929c2ce4c935c9cff85bf11d4',这里上传文件用的是form-data,不能用json

  

 response = requests.post(url=url, headers=headers_from_data, data=file_data).json()

 return response

  

if __name__=='__main__':

  # 上传文件

  res = sendFile(filename, file_path)     # 调用sendFile方法

 print(res)

 

Thanks to everyone who read my article carefully, watching the rise and attention of fans all the way, there is always a need for a gift exchange, although it is not a very valuable thing, if you can use it, you can take it directly:

① More than 2000 Python e-books (mainstream and classic books should be available)

② Python standard library information (the most complete Chinese version)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)


 ⑤ Python学习路线图(告别不入流的学习)

在我的QQ技术交流群里(技术交流和资源共享,广告进来腿给你打断)

可以自助拿走,群号913569736(备注“csdn000”)群里的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。

学习资料可以找到我们呢的蓓蓓小姐姐【mashang-qq】备注【csdn000】免费领取哦

【必须备注】不然不会通过哦

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/122814076