我用这个语法发布了一个带有一些参数的文件:

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

当前回答

也许这个形式可行

curl -X POST -d 'key1=value1&key2=value2' http://URL -H "Content-Type: application/x-www-form-urlencoded"

其他回答

下面的语法可以帮你解决这个问题:

curl -v -F key1=value1 -F upload=@localfilename URL

在Windows 10, curl 7.28.1在powershell,我发现以下工作为我:

$filePath = "c:\temp\dir with spaces\myfile.wav"
$curlPath = ("myfilename=@" + $filePath)
curl -v -F $curlPath URL

使用Smartbear Zephyr Scale(服务器版本),您可以通过以下方式将文件附加到测试周期:

curl -H "Authorization: Basic YkskfdygyzghhMg==" -X POST -H "Content-Type: multipart/form-data" https://jira/jira/rest/atm/1.0/testrun/TDLT-C32/attachments --form file="@file2.txt"

(返回{" id ": 7099})

在file2.txt: Test Cycle中也有一个到测试计划的链接,以及一个到问题的链接。

{
  "projectKey": "TDD",
  "testPlanKey": "TDD-P1",
  "name": "Bonjour chez vous, Le Prisonnier",
  "issueKey": "TDLT-999"
  }

我之所以发布这篇文章,是因为我无法从官方文件中找到答案:)

在Windows中使用curl上传文件,我发现路径需要转义双引号

e.g.

curl -v -F 'upload=@\"C:/myfile.txt\"' URL

我在用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

一切正常!

博士tl;

在多部分边界内容的开头和边界的开头添加换行符(CRLF \r\n),然后重试。

也许您正在向需要更改边界的Java后端发送请求。