python请求-在HTTP请求中没有文件名的POST Multipart/form-data [英] python requests - POST Multipart/form-data without filename in HTTP request

查看:23
本文介绍了python请求-在HTTP请求中没有文件名的POST Multipart/form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 中的请求模块复制以下 POST 请求:

I am trying to replicate the following POST request using the requests module in python:

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 参数.

The documentation for requests suggests that the files argument should be used.

当我尝试以下调用时:

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

我收到以下 HTTP 请求:

I get the following HTTP request:

'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 参数:

I have also tried to use the data argument:

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

导致以下 HTTP 请求:

resulting in the following HTTP request:

'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 标头.

The issue I'm having is that using the files argument results in a call that the server doesn't recognize, presumably due to the unexpected "filename" information sent in the HTTP request. Using the data argument sends the wrong Content-Type header.

已知第一个请求正在我希望将请求发送到的服务器上运行 - 正确复制第一个 HTTP 请求的函数调用是什么?

The first request is known to be working on the server I wish to send the request to - what is the correct function call to identically replicate the first HTTP request?

复制工作请求的示例 HTML 表单:

Sample HTML form to replicate the working request:

<!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 参数传递参数时使用元组:

The solution is to use tuples when passing parameters to the files argument:

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--

这篇关于python请求-在HTTP请求中没有文件名的POST Multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆