问题描述
如何重写以下CURL命令,以使其不使用 -F
选项,但仍会生成完全相同的HTTP请求?即,它直接传递body中的multipart / form-data。
How can I rewrite the following CURL command, so that it doesn't use the -F
option, but still generates the exact same HTTP request? i.e. so that it passes the multipart/form-data in the body directly.
curl -X POST -F example=test http://localhost:3000/test
推荐答案
/ p>
Solved:
curl \
-X POST \
-H "Content-Type: multipart/form-data; boundary=----------------------------4ebf00fbcf09" \
--data-binary @test.txt \
http://localhost:3000/test
其中 test.txt
包含以下文本,最重要的是具有 CRLF(\r\\\
)行结尾:
Where test.txt
contains the following text, and most importantly has CRLF (\r\n) line endings:
------------------------------4ebf00fbcf09
Content-Disposition: form-data; name="example"
test
------------------------------4ebf00fbcf09--
注意:使用 - data-binary
,而不是普通的
-d
,因为前者保留了行结束(这是非常重要的)。另外,请注意,body中的边界以额外的 -
开头。
Notes: it is important to use --data-binary
instead of plain old -d
as the former preserves the line endings (which are very important). Also, note that the boundary in the body starts with an extra --
.
因为它是如此重要,但请求主体文件必须有CRLF行尾。具有良好行结束支持的多平台文本编辑器是jEdit(。
If you're interested in how I worked this out (debugging with a Ruby on Rails app) and not just the final solution, I wrote up my debugging steps on my blog.
这篇关于如何在不使用-F的情况下重写此CURL多部分/表单数据请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!