如何使用cURL将文件内容作为POST参数发送?

Dun*_*all 7 linux bash post curl http

我正在尝试使用cURL来发布文件的内容,就像我将这些内容粘贴到html textarea中一样.这就是说我不想上传文件,我只想要一个名为foo的post参数填充来自名为bar.txt的文件中的文本.bar.txt的内容可能包括换行符,引号等.

这可能吗?

谢谢.

编辑:我最终发现了如何做到这一点:

curl --data-urlencode "foo@bar.txt" http://example.com/index.php
Run Code Online (Sandbox Code Playgroud)

这将获取文件bar.txt的内容,url对其进行编码,将结果字符串放在http://example.com/index.php的POST请求中名为foo的参数中.

我无法谈论其他人建议的解决方案是否有效,但上面的解决方案似乎是最好的方法.

rul*_*rul 4

您可以通过执行以下操作:

$ curl --data "foo:$(cat foo.txt)" http://localhost/yourfile.php
Run Code Online (Sandbox Code Playgroud)

请注意,您可能需要对文件进行编码,如cacheguard所说。要将其编码为 base64,只需修改前面的命令,如下所示:

$ curl --data "foo:$(cat foo.txt | base64)" http://localhost/yourfile.php
Run Code Online (Sandbox Code Playgroud)