簡體   English   中英

python請求 - 在HTTP請求中POST不帶文件名的Multipart / form-data

[英]python requests - POST Multipart/form-data without filename in HTTP request

我試圖使用python中的請求模塊復制以下POST請求:

POST /example/asdfas HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------241652170216373
Content-Length: 279

-----------------------------241652170216373
Content-Disposition: form-data; name="value_1"

12345
-----------------------------241652170216373
Content-Disposition: form-data; name="value_2"

67890
-----------------------------241652170216373--

請求文檔建議應使用files參數。

當我嘗試以下呼叫時:

import requests
requests.post('http://example.com/example/asdfas', files={'value_1': '12345', 
                                                          'value_2': '67890'})

我收到以下HTTP請求:

'Accept': '*/*', 
'Accept-Encoding': 'gzip, deflate, compress', 
'Content-Length': '264', 
'User-Agent': 'python-requests/2.2.1 CPython/3.3.2 Windows/7', 
'Content-Type': 'multipart/form-data; boundary=273f13699c02429db4eb95c97f757d38'
--273f13699c02429db4eb95c97f757d38
Content-Disposition: form-data; name="value_1"; filename="value_1"

12345
--273f13699c02429db4eb95c97f757d38
Content-Disposition: form-data; name="value_2"; filename="value_2"

67890
--273f13699c02429db4eb95c97f757d38--

我也試過使用data參數:

import requests
requests.post('http://example.com/example/asdfas', data={'value_1': '12345', 
                                                         'value_2': '67890'})

導致以下HTTP請求:

'Content-Type': 'application/x-www-form-urlencoded', 
'Content-Length': '27', 
'User-Agent': 'python-requests/2.2.1 CPython/3.3.2 Windows/7', 
'Accept': '*/*', 
'Accept-Encoding': 'gzip, deflate, compress'
value_2=67890&value_1=12345

我遇到的問題是使用files參數導致服務器無法識別的調用,可能是由於HTTP請求中發送的意外“文件名”信息。 使用data參數發送錯誤的Content-Type標頭。

已知第一個請求正在我希望發送請求的服務器上工作 - 正確復制第一個HTTP請求的函數調用是什么?

編輯:示例HTML表單以復制工作請求:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="http://example.com/example/asdfas" method="post" enctype="multipart/form-data">
        <label for="v1">Value 1</label>
        <input id="v1" type="text" name="value_1">
        <label for="v2">Value 2</label>
        <input id="v2" type="text" name="value_2">
        <input type="submit">
    </form>
</body>
</html>

解決方案是在將參數傳遞給files參數時使用元組:

import requests
requests.post('http://example.com/example/asdfas', files={'value_1': (None, '12345'), 'value_2': (None, '67890')})

按預期工作:

'Accept': '*/*', 
'Accept-Encoding': 'gzip, deflate, compress', 
'Content-Length': '228', 
'User-Agent': 'python-requests/2.2.1 CPython/3.3.2 Windows/7', 
'Content-Type': 'multipart/form-data; boundary=85e90a4bbb05474ca1e23dbebdd68ed9'

--85e90a4bbb05474ca1e23dbebdd68ed9
Content-Disposition: form-data; name="value_1"

12345
--85e90a4bbb05474ca1e23dbebdd68ed9
Content-Disposition: form-data; name="value_2"

67890
--85e90a4bbb05474ca1e23dbebdd68ed9--
import requests
from requests_toolbelt import MultipartEncoder

url = 'http://example.com/example/asdfas'
fields = {'value_1':'12345', 'value_2': '67890'}

data = MultipartEncoder(fields=fields)
headers["Content-type"] = m.content_type

requests.post(url=url, data=data, headers=headers)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM