我使用这种语法发布文件以及一些参数:
curl -v -include --form "key1=value1" --form upload=localfilename URL
文件大小约为500K。首先,我看到发送端的内容长度为254。之后服务器响应的内容长度为0。 我哪里错了?
以下是该命令的完整描述。
* Couldn't find host xxx.xxx.xxx.xxx in the _netrc file; using defaults
* About to connect() to xxx.xxx.xxx.xxx port yyyy (#0)
* Trying xxx.xxx.xxx.xxx...
* Adding handle: conn: 0x4b96a0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x4b96a0) send_pipe: 1, recv_pipe: 0
* Connected to xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx) port yyyy (#0)
* POST /zzzzzz/UploadFile HTTP/1.1
* User-Agent: curl/7.32.0
* Host: xxx.xxx.xxx.xxx:yyyy
* Accept: */*
* Content-Length: 254
* Expect: 100-continue
* Content-Type: multipart/form-data; boundary=------------------------948a6137eef50079
*
* HTTP/1.1 100 Continue
* HTTP/1.1 100 Continue
* HTTP/1.1 200 OK
* HTTP/1.1 200 OK
* Server Apache-Coyote/1.1 is not blacklisted
* Server: Apache-Coyote/1.1
* Server: Apache-Coyote/1.1
* Added cookie JSESSIONID="C1D7DD042E250211D9DEA82688876F88" for domain xxx.xxx.xxx.xxx, path /zzzzz/, expire 0
* Set-Cookie: JSESSIONID=C1D7DD042E250211D9DEA82688876F88; Path=/zzzzzz/;
* HttpOnly
* Set-Cookie: JSESSIONID=C1D7DD042E250211D9DEA82688876F88; Path=/zzzzzz/; HttpOnly
* Content-Type: text/html;charset=ISO-8859-1
Content-Type: text/html;charset=ISO-8859-1
* Content-Length: 0
* Content-Length: 0
* Date: Tue, 01 Oct 2013 11:54:24 GMT
* Date: Tue, 01 Oct 2013 11:54:24 GMT
* Connection #0 to host xxx.xxx.xxx.xxx left intact
答案 0 :(得分:196)
以下语法为您解决了这个问题:
curl -v -F key1=value1 -F upload=@localfilename URL
答案 1 :(得分:14)
在Windows中使用curl上传文件我发现该路径需要转义双引号
e.g。
curl -v -F 'upload=@\"C:/myfile.txt\"' URL
答案 2 :(得分:5)
这对我有用
__author__ = 'Robert'
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello BBck!"
@app.route("/welcome")
def welcome():
return render_template("welcome.html")
if __name__ == "__main__":
app.run(debug=True)
答案 3 :(得分:0)
在Windows 10上,在powershell中卷曲7.28.1,我发现以下内容对我有用:
$filePath = "c:\temp\dir with spaces\myfile.wav"
$curlPath = ("myfilename=@" + $filePath)
curl -v -F $curlPath URL
答案 4 :(得分:0)
我很难用curl
向Java后端发送多部分HTTP PUT请求。我只是尝试了
curl -X PUT URL \
--header 'Content-Type: multipart/form-data; boundary=---------BOUNDARY' \
--data-binary @file
文件内容为
-----------BOUNDARY
Content-Disposition: form-data; name="name1"
Content-Type: application/xml;version=1.0;charset=UTF-8
<xml>content</xml>
-----------BOUNDARY
Content-Disposition: form-data; name="name2"
Content-Type: text/plain
content
-----------BOUNDARY--
但是我总是得到一个错误,即边界不正确。在进行一些Java后端调试之后,我发现Java实现正在向边界添加\r\n--
作为前缀,因此将输入文件更改为
<-- here's the CRLF
-------------BOUNDARY <-- added '--' at the beginning
...
-------------BOUNDARY <-- added '--' at the beginning
...
-------------BOUNDARY-- <-- added '--' at the beginning
一切正常!
在多部分边界内容的开头添加换行符(CRLF \r\n
),在边界的开头添加--
,然后重试。
也许您正在向Java后端发送一个需要边界更改的请求。