如何在不使用-F的情况下重写此CURL多部分/表单数据请求? [英] How can I rewrite this CURL multipart/form-data request without using -F?

查看:239
本文介绍了如何在不使用-F的情况下重写此CURL多部分/表单数据请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重写以下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(如何

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆