【问题标题】:Multipart POST request in PythonPython中的多部分POST请求
【发布时间】:2020-04-25 14:09:17
【问题描述】:

我有以下 curl 命令在终端中完美运行

curl --location --request POST 'https://online.tet_url' --form license='test_licence' --form product='pdfserver' --form readUSdates='true' --form pdf-filename='@/dir/test.pdf'

请注意,pdf-filename 是指 pdf 文件在我的本地计算机上的位置。 当我尝试使用 python 请求包时:

url = "https://online.tet_url"
payload = {"license": 'test_licence',
'product': 'pdfserver',
'readUSdates': 'true',
'pdf-filename': '@/dir/test.pd'}
headers = {
  'Content-Type': 'multipart/form-data'
}
response = requests.request("POST", url, headers=headers, data = payload)

我收到 400 错误并显示以下消息:

++++++

Apache Tomcat/8.0.32 - 错误报告H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma ,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;字体大小:14px;} 正文 {字体系列:Tahoma,Arial,无衬线;颜色:黑色;背景颜色:白色;} B {字体系列:Tahoma,Arial,无衬线;颜色:白色;背景颜色:#525D76;} P {字体系列:Tahoma,Arial,无衬线;背景:白色;颜色:黑色;字体大小:12px;}A {颜色:黑色;}A.name {颜色:黑色;}.line {高度:1px;背景颜色:#525D76;边框:无;}

HTTP 状态 415 - 不支持的媒体类型

类型状态报告

消息不支持的媒体类型

说明服务器拒绝了这个请求,因为请求实体的格式不受所请求方法的请求资源支持。

Apache Tomcat/8.0.32

++++++++

请注意,我尝试将其导入 Postman,但当我从那里运行它时,它不起作用。 我尝试使用 https://curl.haxx.se/docs/manpage.html 将其转换为 python 请求,但它不再起作用。

如果您能帮助我使用 Python 发布此 curl,我将不胜感激。

【问题讨论】:

    标签: python-3.x curl post request python-requests


    【解决方案1】:

    对于上传文件,我们应该使用单独的包

    from pathlib import Path
    
    url = 'https://online.tet_url'
    data = {
            'license': 'test_licence',
            'product': 'pdfserver',
            'readUSdates': 'true }
    files = {
            'pdf-filename': Path(directory).open('rb'),
            }
    response = requests.request('POST', url, data=data, files=files)
    
    

    【讨论】:

      猜你喜欢
      • 2013-11-26
      • 2019-02-19
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 2016-10-19
      相关资源
      最近更新 更多