首页 > 编程语言 >python + requests:请求头('Content-Type': 'multipart/form-data'),并且files和其他参数均正确定义,但

python + requests:请求头('Content-Type': 'multipart/form-data'),并且files和其他参数均正确定义,但

时间:2023-06-30 20:13:59浏览次数:49  
标签:files 参数均 Content headers test MultipartEncoder multipart 13.6 requests

解释

1. 在使用'Content-Type': 'multipart/form-data'上传文件时,你需要将文件作为请求体的一部分进行传输。而由于文件可能非常大,因此需要将文件分成多个部分进行传输。这就需要使用多部分编码(Multipart Encoding)来将文件分割成多个部分,并将每个部分与其它的表单数据一起打包到一个请求体中。
2. MultipartEncoder是一个Python库,它可以将请求体分割成多个部分,并为每个部分添加必要的头信息。这样,你就可以将请求体作为一个整体进行上传,而不必手动将其分割成多个部分。使用MultipartEncoder可以方便地处理文件上传时的多部分编码,因此它是上传文件时的常用工具之一。
3. 因此,在使用'Content-Type': 'multipart/form-data'上传文件时,你需要使用MultipartEncoder将请求体包装起来,以便服务器能够正确地解析请求体并处理文件内容。

代码示例 (方式一)

from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests

def test_files():
    url = "https://../all/versions?abortWhenHeartbeatException=yes"

    files = ('zc_test_1.0.process.zip',
             open('/Users/.../resource/zc_test_1.0.process.zip', 'rb'),
             'application/octet-stream')

    payload = {
        # ...
        "studioVersion": "13.6.0-13.6.0%0D",
        "visualVars": "%5B%5D",
        "outputParams": "%5B%5D",
        "file": files,
    }
    headers = {
        'x-referer': '**',
        'x-version': '13.6.0',
        'Accept-Language': 'zh-CN',
        'Authorization': '***',
        'X-Tenant': ''
    }

    mutipart_encoder = MultipartEncoder(fields=payload)
    headers['Content-Type'] = mutipart_encoder.content_type
    response = requests.request("POST", url, headers=headers, data=mutipart_encoder)
    print(response.text)

代码示例(方式二)

  def test_files():
      url = "https://../versions?abortWhenHeartbeatException=yes"

      files = {
          "file": (
              'zc_test_1.0.process.zip', open('/Users/../resource/zc_test_1.0.process.zip', 'rb'), 'application/zip')

      }
      payload = {
          # ...
          "studioVersion": "13.6.0-13.6.0%0D",
          "visualVars": "%5B%5D",
          "outputParams": "%5B%5D",
      }
      headers = {
          'x-referer': '**',
          'x-version': '13.6.0',
          'Accept-Language': 'zh-CN',
          'Authorization': '**',
          'X-Tenant': '**',
      }
      response = requests.request("POST", url, headers=headers, data=payload, files=files)
      print(response.text)

标签:files,参数均,Content,headers,test,MultipartEncoder,multipart,13.6,requests
From: https://www.cnblogs.com/luckyqh/p/17510438.html

相关文章

  • Tips: How to Get the Currently Selected HTML Content in Major Browsers
    functiongetSelectionHtml(){constsel=window.getSelection();if(sel.rangeCount){varcontainer=document.createElement("div");for(vari=0,len=sel.rangeCount;i<len;++i){container.appendCh......
  • jmeter中http请求常用的content-type
    Content-Type: application/json   --json格式Content-Type: application/x-www-form-urlencoded--表单格式 当使用表单格式时,需要注意勾选URLEncode和IncludeEquals ......
  • Android adb shell content命令
      adbshellcontentusage:adbshellcontent[subcommand][options]usage:adbshellcontentinsert--uri<URI>[--user<USER_ID>]--bind<BINDING>[--bind<BINDING>...][--extra<BINDING>...] <URI>acontentprovide......
  • Delete vector contents and free up memory in C++
     DeletevectorcontentsandfreeupmemoryinC++Thispostwilldiscusshowtodeletethevector’scontentsandfreeupthememoryallocatedbythevectortostoreobjectsinC++.1.Using vector::clear functionWecanusethe vector::clear......
  • [HTML5] Content Security Policy CSP Header
    default-src"none";script-src"self";img-src"self"example.com;style-srcfonts.googleapis.com;font-srcfonts.gstatic.com;<scriptsrc="/js/app.js"></script>:allowbecause script-src"self&qu......
  • 有了它,再也不用写setContentView了!好用,真的好用~
    前言大家多多少少都用过或者看过注解(Annotation),比如最常见@Override、@Deprecated等。近年来一些比较流行的三方框架都使用的注解,像ButterKnife(渐渐被Databinding、ViewBinding取代,已经停止维护)、Dagger、Room等等。那为什么这些大牛都这么热衷于使用注解呢?原因肯定是注解的好处多......
  • header("Content-type:text/html;charset=utf-8")
    它的意思是设置页面内容是html,编码格式是utf-8。header()函数的作用是:发送一个原始HTTP标头[HttpHeader]到客户端。标头(header)是服务器以HTTP协义传HTML资料到浏览器前所送出的字串,在标头与HTML文件之间尚需空一行分隔。在PHP中送回HTML资料前,需先传完所有的标......
  • 什么是 SAP Commerce Cloud 的 Content Catalog 概念
    SAPCommerceCloud的ContentCatalog是一个核心概念,用于管理和组织网站内容。ContentCatalog是一个包含页面、组件和其他相关资源的容器,它允许您以结构化和可重用的方式创建和管理网站的不同部分。在本文中,我们将详细介绍ContentCatalog的概念、用途和示例。ContentCatalog的......
  • 什么是 SAP Commerce Cloud 的 Content Slot
    内容槽(Contentslots)是组件的容器。要使组件在前端页面上显示,必须将其添加到内容槽中。内容槽分为三种类型:页面内容槽(Pagecontentslots):页面内容槽,也简称为槽,属于所在页面,即属于该页面实例。当向槽添加内容时,内容将显示在包含该槽的页面上。共享内容槽(Sharedcontentslots):共......
  • setContentView(R.layout.activity_);ui->setupUi(this);
    publicclassTutorialPartIIIextendsActivity{@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);//setContentView(R.layout.activity_tutorial_part_iii);//*......