在本文中,我们将详细介绍在Appenginepython中使用multipart/form-data发布请求不起作用的各个方面,并为您提供关于python发送post请求类型为formdata的相关解
在本文中,我们将详细介绍在Appengine python中使用multipart / form-data发布请求不起作用的各个方面,并为您提供关于python发送post请求类型为formdata的相关解答,同时,我们也将为您带来关于application / x-www-form-urlencoded或multipart / form-data?、application/x-www-form-urlencode/multipart/form-data、application/x-www-form-urlencoded or multipart/form-data?、application/x-www-form-urlencoded和multipart/form-data的有用知识。
本文目录一览:- 在Appengine python中使用multipart / form-data发布请求不起作用(python发送post请求类型为formdata)
- application / x-www-form-urlencoded或multipart / form-data?
- application/x-www-form-urlencode/multipart/form-data
- application/x-www-form-urlencoded or multipart/form-data?
- application/x-www-form-urlencoded和multipart/form-data
在Appengine python中使用multipart / form-data发布请求不起作用(python发送post请求类型为formdata)
我正在尝试将来自appengine应用的分段请求发送到dotcloud上托管的外部(django)api。该请求包含一些文本和文件(pdf),并使用以下代码发送
from google.appengine.api import urlfetch
from poster.encode import multipart_encode
from libs.poster.streaminghttp import register_openers
register_openers()
file_data = self.request.POST['file_to_upload']
the_file = file_data
send_url = "http://127.0.0.1:8000/"
values = {
'user_id' : '12341234','the_file' : the_file
}
data,headers = multipart_encode(values)
headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
data = str().join(data)
result = urlfetch.fetch(url=send_url,payload=data,method=urlfetch.POST,headers=headers)
logging.info(result.content)
当此方法运行时,Appengine会给出以下警告(我不确定它是否与我的问题有关)
Stripped prohibited headers from URLFetch request: ['Content-Length']
然后Django发送以下错误
<class 'django.utils.datastructures.MultiValueDictKeyError'>"Key 'the_file' not found in <MultiValueDict: {}>"
django代码非常简单,当我使用邮递员chrome扩展名发送文件时可以使用。
@csrf_exempt
def index(request):
try:
user_id = request.POST["user_id"]
the_file = request.FILES["the_file"]
return HttpResponse("OK")
except:
return HttpResponse(sys.exc_info())
如果我加
print request.POST.keys()
我得到一个包含user_id和the_file的字典,指示该文件未作为文件发送。如果我对文件执行相同的操作,即
print request.FILES.keys()
我得到空列表[]。
编辑1:
我已更改问题以实施某人的建议,但这仍然失败。我还添加了Glenn发送的链接推荐的标头附加内容,但并不令人高兴。
编辑2:
我也尝试过发送the_file作为
the_file = file_data.file
the_file = file_data.file.read()
但是我得到了同样的错误。
编辑3:
我也尝试过将Django应用编辑为
the_file = request.POST["the_file"]
但是,当我尝试使用
path = default_storage.save(file_location,ContentFile(the_file.read()))
它失败了
<type 'exceptions.AttributeError'>'unicode' object has no attribute 'read'<traceback object at 0x101f10098>
类似地,如果我尝试访问the_file.file(可以在我的appengine应用中访问),它会告诉我
<type 'exceptions.AttributeError'>'unicode' object has no attribute 'file'<traceback object at 0x101f06d40>
application / x-www-form-urlencoded或multipart / form-data?
问题:
In HTTP there are two ways to POST data: application/x-www-form-urlencoded
and multipart/form-data
. 在HTTP中有两种POST数据的方式: application/x-www-form-urlencoded
和multipart/form-data
。 I understand that most browsers are only able to upload files if multipart/form-data
is used. 据我所知,如果使用multipart/form-data
,大多数浏览器只能上传文件。 Is there any additional guidance when to use one of the encoding types in an API context (no browser involved)? 在API上下文中使用其中一种编码类型时是否有任何其他指导(不涉及浏览器)? This might eg be based on: 这可能基于:
- data size 数据大小
- existence of non-ASCII characters 存在非ASCII字符
- existence on (unencoded) binary data 存在于(未编码的)二进制数据上
- the need to transfer additional data (like filename) 需要传输额外的数据(如文件名)
I basically found no formal guidance on the web regarding the use of the different content-types so far. 到目前为止,我基本上没有在网上找到有关使用不同内容类型的正式指导。
解决方案:
参考一: https://stackoom.com/question/Goef/application-x-www-form-urlencoded或multipart-form-data参考二: https://oldbug.net/q/Goef/application-x-www-form-urlencoded-or-multipart-form-data
application/x-www-form-urlencode/multipart/form-data
首先我们先认识下今天的application/x-www-form-urlencode/multipart/form-data属性所在的位置
1、form所属
在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。 例如: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,这个一般文件上传时用。 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
2、不同的编码方式
form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。
当数据传递的方式是get的时候,浏览器使用application/x-www-form-urlencode的编码方式,把form数据转换为一个字符串,如(name=zhangsan&age=128),然后把这个字符添加到url后面,用?连接 组成新的url并加载
当数据传递的方式是post的时候 浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。(ps:第二种方式的解释本人还有些模糊,有更为精确答案的猿,希望分享下)
application/x-www-form-urlencoded or multipart/form-data?
TL;DR
The moral of the story is, if you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, use multipart/form-data
. Otherwise, use application/x-www-form-urlencoded
.
The MIME types you mention are the two Content-Type
headers for HTTP POST requests that user-agents (browsers) must support. The purpose of both of those types of requests is to send a list of name/value pairs to the server. Depending on the type and amount of data being transmitted, one of the methods will be more efficient than the other. To understand why, you have to look at what each is doing under the covers.
For application/x-www-form-urlencoded
, the body of the HTTP message sent to the server is essentially one giant query string -- name/value pairs are separated by the ampersand (&
), and names are separated from values by the equals symbol (=
). An example of this would be:
MyVariableOne=ValueOne&MyVariableTwo=ValueTwo
According to the specification:
[Reserved and] non-alphanumeric characters are replaced by `%HH'', a percent sign and two hexadecimal digits representing the ASCII code of the character
That means that for each non-alphanumeric byte that exists in one of our values, it''s going to take three bytes to represent it. For large binary files, tripling the payload is going to be highly inefficient.
That''s where multipart/form-data
comes in. With this method of transmitting name/value pairs, each pair is represented as a "part" in a MIME message (as described by other answers). Parts are separated by a particular string boundary (chosen specifically so that this boundary string does not occur in any of the "value" payloads). Each part has its own set of MIME headers like Content-Type
, and particularly Content-Disposition
, which can give each part its "name." The value piece of each name/value pair is the payload of each part of the MIME message. The MIME spec gives us more options when representing the value payload -- we can choose a more efficient encoding of binary data to save bandwidth (e.g. base 64 or even raw binary).
Why not use multipart/form-data
all the time? For short alphanumeric values (like most web forms), the overhead of adding all of the MIME headers is going to significantly outweigh any savings from more efficient binary encoding.
application/x-www-form-urlencoded和multipart/form-data


今天的关于在Appengine python中使用multipart / form-data发布请求不起作用和python发送post请求类型为formdata的分享已经结束,谢谢您的关注,如果想了解更多关于application / x-www-form-urlencoded或multipart / form-data?、application/x-www-form-urlencode/multipart/form-data、application/x-www-form-urlencoded or multipart/form-data?、application/x-www-form-urlencoded和multipart/form-data的相关知识,请在本站进行查询。
本文标签: