Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1013444
  • 博文数量: 157
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1388
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-09 15:37
文章分类

全部博文(157)

文章存档

2023年(9)

2022年(2)

2021年(18)

2020年(7)

2017年(13)

2016年(53)

2015年(55)

我的朋友

分类: LINUX

2021-12-01 16:41:36

根据post请求头的content-type不同而不同:
1. content-type为"application/x-www-form-urlencoded":

点击(此处)折叠或打开

  1. from urllib import parse
  2. headers = {'content-type': 'application/x-www-form-urlencoded'}
  3. url = "{0}".format(token)
  4. with open(path, "rb") as video:
  5.     base64_video = base64.b64encode(video.read()).decode()
  6. content = {'type': 'video','video_base64': base64_video}
  7. data= parse.urlencode(content)
  8. try:
  9.     response = requests.post(url=url,
  10.                                  data=data,
  11.                                  headers=headers,
  12.                                  )
  13.     res = response.json()
  14.     msg = res.get("error_msg")
  15.     xxx
  16. except Exception as e:
  17.     xxx

2.content-type为"application/json":

点击(此处)折叠或打开

  1. headers = {'content-type': 'application/json'}
  2. url = "{0}".format(token)
  3. with open(path, "rb") as video:
  4.     base64_video = base64.b64encode(video.read()).decode()
  5. try:
  6.     response = requests.post(url=url,
  7.                                  data=json.dumps([{
  8.                                      "video": base64_video ,
  9.                                      "type": "BASE64",
  10.                                  }]),
  11.                                  headers=headers,
  12.                                  )
  13.     res = response.json()
  14.     msg = res.get("error_msg")
  15.     xxx
  16. except Exception as e:
  17.     xxx

3.
content-type为"form-data":

点击(此处)折叠或打开

  1. headers = {
  2.     "Cookie": "session_id=xxxxxx"
  3. }
  4. url = "{0}/df/upload".format(host)
  5. content_type = "video/mp4"
  6. #content_type = "image/jpg"
  7. files = {
  8.     "file": (os.path.basename(file_path), open(file_path, "rb"), content_type),
  9. }
  10. data = {
  11.      "type": 1
  12. }
  13. if tid:
  14.     data["tid"] = tid
  15.  
  16. try:
  17.     response = requests.post(url, files=files, data=data, headers=headers)
  18.     res = response.json()
  19.     msg = res.get("error_msg")
  20.     xxx
  21. except Exception as e:
  22.     xxx

阅读(2936) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~