如何在不使用 -F 的情况下重写此 CURL 多部分/表单数据请求? [英] How can I rewrite this CURL multipart/form-data request without using -F?
问题描述
如何重写以下 CURL 命令,使其不使用 -F
选项,但仍生成完全相同的 HTTP 请求?即,以便它直接在正文中传递 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
推荐答案
已解决:
curl
-X POST
-H "Content-Type: multipart/form-data; boundary=----------------------------4ebf00fbcf09"
--data-binary @test.txt
http://localhost:3000/test
其中 test.txt
包含以下文本,最重要的是具有 CRLF (
) 行尾:
Where test.txt
contains the following text, and most importantly has CRLF (
) line endings:
------------------------------4ebf00fbcf09
Content-Disposition: form-data; name="example"
test
------------------------------4ebf00fbcf09--
注意:使用 --data-binary
而不是普通的 -d
很重要,因为前者保留了行尾(这非常重要).另请注意,正文中的边界以额外的 --
开头.
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(how to在 jEdit 中设置行尾).
I'm going to repeat it because it's so important, but that request-body file must have CRLF line endings. A multi-platform text editor with good line-ending support is jEdit (how to set the line endings in jEdit).
如果您对我是如何解决这个问题的(使用 Ruby on Rails 应用程序调试)而不只是最终解决方案感兴趣,我在 我的博客.
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 多部分/表单数据请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!