【问题标题】:POST file upload causes HTTP 400 error responsePOST 文件上传导致 HTTP 400 错误响应
【发布时间】:2017-07-23 18:31:59
【问题描述】:

我正在尝试使用 Python 中的 POST 请求(请求库)上传文件。 我收到HTTP 400 error 作为回复。我认为这是因为我的 POST 请求不是应有的格式。任何想法如何将 POST 请求 1 转换为 2?

代码:

files = {"file": ("test-file.txt", open("c:/users/johndoe/desktop/test-file.txt", "rb"), "text/plain")}
response = webdriver.request("POST", "https://something.com/attachments", files = files)

我目前拥有的 (1):

--d9bd23df892242a489b0f638d62502a6
Content-Disposition: form-data; name="file"; filename="test-file.txt"
Content-Type: text/plain

This is a test file.

Regards,

John Doe
--d9bd23df892242a489b0f638d62502a6--

我应该拥有的(2):

-----------------------------26789175756830
Content-Disposition: form-data; name="fileName"

test-file.txt
-----------------------------26789175756830
Content-Disposition: form-data; name="fileSize"

45
-----------------------------26789175756830
Content-Disposition: form-data; name="description"

undefined
-----------------------------26789175756830
Content-Disposition: form-data; name="file"; filename="test-file.txt"
Content-Type: text/plain

This is a test file.

Regards,

John Doe
-----------------------------26789175756830--

我认为我需要补充的(3):

-----------------------------26789175756830
Content-Disposition: form-data; name="fileName"

test-file.txt
-----------------------------26789175756830
Content-Disposition: form-data; name="fileSize"

45
-----------------------------26789175756830
Content-Disposition: form-data; name="description"

undefined
-----------------------------26789175756830

【问题讨论】:

  • 是什么让您认为需要指定“fileName”、“fileSize”和“description”字段? Content-Disposition: form-data; name="file"; filename="test-file.txt" 正是浏览器为多部分编码形式的 <input> 类型文件生成的内容。
  • 因为我已经将我的请求与来自网站的请求进行了比较,我发现的唯一区别就是这部分。
  • 我认为服务器需要这种格式。你觉得会是这样吗?
  • 当然,它可以使用这种格式。我只是想知道你从哪里得到格式。我想也许您使用的网站有一些解释它的文档。
  • @ThisSuitIsBlackNot 不,不幸的是没有,但是知道如何将这部分添加到我的代码中吗?

标签: python file http upload python-requests


【解决方案1】:

您必须明确指定字段fileNamefileSizedescription,因为请求不会自动为您生成它们:

import os
import requests

filepath = 'foo'
files = {'file': open(filepath, 'rb')}
data = {
    'fileName': filepath,
    'fileSize': os.path.getsize(filepath),
    'description': 'undefined'
}

response = requests.post('http://www.example.com/', data=data, files=files);

这会生成如下请求:

POST / HTTP/1.1
Connection: keep-alive
Accept: */*
Accept-Encoding: gzip, deflate
Host: www.example.com
User-Agent: python-requests/2.13.0
Content-Length: 439
Content-Type: multipart/form-data; boundary=75a7213b6b4f493fabe26feeafb8551c
Http.Entity.Parser.Multipart.Tempdir: /tmp/pZjDE

--75a7213b6b4f493fabe26feeafb8551c
Content-Disposition: form-data; name="description"

undefined
--75a7213b6b4f493fabe26feeafb8551c
Content-Disposition: form-data; name="fileSize"

16
--75a7213b6b4f493fabe26feeafb8551c
Content-Disposition: form-data; name="fileName"

foo
--75a7213b6b4f493fabe26feeafb8551c
Content-Disposition: form-data; name="file"; filename="foo"

contents of foo

--75a7213b6b4f493fabe26feeafb8551c--

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2016-09-22
    • 2019-12-02
    • 2020-11-05
    • 2017-10-09
    • 2014-05-14
    相关资源
    最近更新 更多