43

如何重写以下 CURL 命令,使其不使用该-F选项,但仍生成完全相同的 HTTP 请求?即它直接传递正文中的多部分/表单数据。

curl -X POST -F example=test http://localhost:3000/test
4

7 回答 7

74

解决了:

curl \
  -X POST \
  -H "Content-Type: multipart/form-data; boundary=----------------------------4ebf00fbcf09" \
  --data-binary @test.txt \
  http://localhost:3000/test

其中test.txt包含以下文本,最重要的是具有CRLF (\r\n) 行尾

------------------------------4ebf00fbcf09
Content-Disposition: form-data; name="example"

test
------------------------------4ebf00fbcf09--

注意:重要的是使用--data-binary而不是普通旧-d的,因为前者保留了行尾(这非常重要)。另外,请注意正文中的边界以额外的--.

我要重复一遍,因为它非常重要,但是请求正文文件必须有 CRLF 行结尾。具有良好行尾支持的多平台文本编辑器是 jEdit(如何在 jEdit 中设置行尾)。

如果您对我如何解决这个问题(使用 Ruby on Rails 应用程序进行调试)感兴趣,而不仅仅是最终解决方案,我在我的博客上写下了我的调试步骤。

于 2012-05-26T09:57:17.753 回答
31

您可以将--form参数与显式

curl -H "Content-Type: multipart/related" \
  --form "data=@example.jpg;type=image/jpeg" http://localhost:3000/test
于 2012-12-11T18:56:44.690 回答
16

这是一个替代答案,将原始 CURL 语句重写-d为单行,没有临时文件。我个人认为临时文件方法更容易理解,但我也将其放在这里以供参考:

curl -X POST -H "Content-Type: multipart/form-data; boundary=----------------------------4ebf00fbcf09" -d $'------------------------------4ebf00fbcf09\r\nContent-Disposition: form-data; name="example"\r\n\r\ntest\r\n------------------------------4ebf00fbcf09--\r\n' http://localhost:3000/test

注意:$'blar'语法使得 bash 将 \r\n 解析为 CRLF 标记。感谢这个提示的答案

于 2012-05-26T10:30:01.170 回答
5

这就是我正在使用的,我认为它很干净,不需要临时文件,也不需要占用 RAM,以防你想上传整个文件(所以不要将文件读入内存)。

# Set these two.
file='path/to/yourfile.ext'
url='http://endpoint.example.com/foo/bar'

delim="-----MultipartDelimeter$$$RANDOM$RANDOM$RANDOM"
nl=$'\r\n'
mime="$(file -b --mime-type "$file")"

# This is the "body" of the request.
data() {
    # Also make sure to set the fields you need.
    printf %s "--$delim${nl}Content-Disposition: form-data; name=\"userfile\"${nl}Content-Type: $mime$nl$nl"
    cat "$file"
    printf %s "$nl--$delim--$nl"
}

# You can later grep this, or something.
response="$(data | curl -# "$url" -H "content-type: multipart/form-data; boundary=$delim" --data-binary @-)"
于 2016-02-24T08:24:14.230 回答
1

这是使用“Content-Type:multipart/related”上传一个图像文件,

curl --trace trace.txt -X POST -H 'Content-Type: multipart/related; boundary=boundary_1234' --data-binary $'--boundary_1234\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n{\r\n\t"title": "TestFile"\r\n}\r\n\r\n--boundary_1234\r\nContent-Type: image/jpeg\r\n\r\n' --data-binary '@Image0177.jpg' --data-binary $'\r\n--boundary_1234--\r\n' 'http://localhost:3000/google/upload/drive/v2/files?uploadType=multipart'
于 2014-04-03T05:47:49.673 回答
0

这是我的做法:

curl https://httpbin.org/post \
    -H 'content-type: multipart/form-data; boundary=----FormBoundary123456789' \
    --data-binary $'------FormBoundary123456789\r
Content-Disposition: form-data; name="example"\r
\r
test\r
------FormBoundary123456789--\r
'

或者更复杂一点(应该可以移植到大多数现代 shell):

DELIM=----FormBoundary$RANDOM$RANDOM

curl https://httpbin.org/post \
    -H "content-type: multipart/form-data; boundary=$DELIM" \
    --data-binary --$DELIM$'\r
Content-Disposition: form-data; name="example"\r
\r
test\r
'--$DELIM--$'\r
'
于 2018-09-19T13:41:09.833 回答
-3

这适用于多部分/表单数据请求方法。上传文件添加 --form filename="@path/image.jpg;type=image/jpeg"

curl --form key="value" --form key="value" http://localhost:3000/test

于 2016-01-19T11:01:41.640 回答