如何重写以下 CURL 命令,使其不使用该-F
选项,但仍生成完全相同的 HTTP 请求?即它直接传递正文中的多部分/表单数据。
curl -X POST -F example=test http://localhost:3000/test
如何重写以下 CURL 命令,使其不使用该-F
选项,但仍生成完全相同的 HTTP 请求?即它直接传递正文中的多部分/表单数据。
curl -X POST -F example=test http://localhost:3000/test
解决了:
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 应用程序进行调试)感兴趣,而不仅仅是最终解决方案,我在我的博客上写下了我的调试步骤。
您可以将--form
参数与显式
curl -H "Content-Type: multipart/related" \
--form "data=@example.jpg;type=image/jpeg" http://localhost:3000/test
这是一个替代答案,将原始 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 标记。感谢这个提示的答案。
这就是我正在使用的,我认为它很干净,不需要临时文件,也不需要占用 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 @-)"
这是使用“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'
这是我的做法:
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
'
这适用于多部分/表单数据请求方法。上传文件添加 --form filename="@path/image.jpg;type=image/jpeg"
curl --form key="value" --form key="value" http://localhost:3000/test