当前位置: 首页 > news >正文

python的requests请求参数带files

踩坑接口请求参数含文件

  • requests
    • 接口请求既有file,也有json。划重点params

requests

官网地址
https://requests.readthedocs.io/en/stable/user/quickstart/#post-a-multipart-encoded-file

接口请求既有file,也有json。划重点params

import requestsurl = "https://xx.xxxxx.com/api"# json类型的请求参数
payload = {'type': '123','doctype': '1'}
# 文件参数,注意files一定是列表啊,官网写的字典不行!!!
# 比较优雅的写的话,file最好用with open啊,自动colse。
#       with open('xxx/xxx.docx', 'rb') as f:
#            files = [
#               ('file', ('xxx.docx',
#                          f,
#                          'application/vnd.openxmlformats-#officedocument.wordprocessingml.document'))
#            ]
files=[('file',('xxx.docx',open('/xxx/xxx/xxx/xxx.docx','rb'),'application/vnd.openxmlformats-officedocument.wordprocessingml.document'))
]
# 请求头类型-文件
headers = {'Content-Type': 'multipart/form-data',}# 重点是,用params传json。如果用data就不行
response = requests.request("POST", url, headers=headers, params=payload, files=files)print(response.text)
http://www.lryc.cn/news/248119.html

相关文章:

  • Elk:filebeat 日志收集工具和logstash
  • [设计模式] 常见的设计模式
  • 报错解决:You may need an additional loader to handle the result of these loaders.
  • 配置自动化部署Jenkins和Gitea
  • VSCODE+QEMU+WSL调试RISCV代码(SBI、kernel)
  • 二叉树(判断是否为对称二叉树)
  • STM32开发学习(地址映射)
  • 证明E(X+Y) =E(X) + E(Y)
  • ClickHouse入门手册1.0
  • 10个火爆的设计素材网站推荐
  • SQL注入 - CTF常见题型
  • android keylayout键值适配
  • python读取excel自动化生成sql建表语句和java实体类字段
  • Unity求向量A在平面L上的投影向量
  • 人机交互2——任务型多轮对话的控制和生成
  • 【数据结构】八大排序 (三)
  • Redis 命令处理过程
  • python爬虫进阶教程之如何正确的使用cookie
  • 【hacker送书第4期】推荐4本Java必读书籍(各送一本)
  • [密码学]DES
  • 15个超级实用的Python操作,肯定有你意想不到的!
  • GitHub上8个强烈推荐的 Python 项目
  • 什么是依赖倒置原则
  • 异常数据检测 | Python实现oneclassSVM模型异常数据检测
  • using meta-SQL 使用元SQL (3)
  • Spinnaker 基于 docker registry 触发部署
  • 2023亚马逊云科技re:Invent,在开发者板块探究如何利用技术重塑业务
  • JAVA 使用stream流将List中的对象某一属性创建新的List
  • Elasticsearch:ES|QL 函数及操作符
  • SpringBoot——Swagger2 接口规范