我试图模拟浏览器工作。当我从浏览器发送图像时,我会收到请求。文件
<MultiValueDict: {u'file': [<InMemoryUploadedFile: 1.jpg (image/jpeg)>]}>
如果我从后端POST请求发送图像:
import httplib
conn = httplib.HTTPConnection(HOST, PORT)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": 'text/plain',
"Cookie": cookies, 'X_REQUESTED_WITH': 'XMLHttpRequest'}
conn.request("POST", "/upload/%s" % storyId, open("pictures/1.jpg"), headers)
response = conn.getresponse()
我明白了:
<MultiValueDict: {}>
出了什么问题?
答案 0 :(得分:0)
当您从broswer发送文件时,Content Type
标题为multipart/form-data
;不application/x-www-form-urlencoded
发送文件时请求中还有其他更改。看一下,这是从浏览器发送文件时请求标头的样子。
POST /upload?upload_progress_id=12344 HTTP/1.1
Host: localhost:3000
Content-Length: 1325
Origin: http://localhost:3000
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryePkpFF7tjBAqx29L
<other headers>
------WebKitFormBoundaryePkpFF7tjBAqx29L
Content-Disposition: form-data; name="MAX_FILE_SIZE"
100000
------WebKitFormBoundaryePkpFF7tjBAqx29L
Content-Disposition: form-data; name="uploadedfile"; filename="hello.o"
Content-Type: application/x-object
<file data>
------WebKitFormBoundaryePkpFF7tjBAqx29L--
您可以在此stackoverflow answer
中阅读更多内容说,您没有正确“模拟”浏览器文件请求。