Unix使用文件中的内容将cURL POST发送到特定变量

Rim*_*mer 6 unix bash post curl

我已经搜索过这个答案,但没有发现任何有效或完全符合我问题的东西.

使用Unix cURL,我需要将一个键/值对POST到服务器.密钥将是"MAC",并且换行符分隔的MAC地址文件的内容将是此POST的值.

我试过了:

curl -d @filename http://targetaddress.com,但是当目标收到传入的POST时,POST var为空.(PHP正在接收).

我已经看到在这个网站上提到的其他形式的卷曲,使用--url-encode它表示它不是我的系统卷曲的有效选项...

如何使用UNIX cURL将文件内容作为值发布到POST中的特定键?

boh*_*ica 4

根据curl手册页,-d与--data-ascii相同。要将数据作为二进制发布,请使用 --data-binary 并使用 url 编码发布,请使用 --data-urlencode。因此,由于您的文件没有经过 URL 编码,如果您想发送 URL 编码的文件,请使用:

curl --data-urlencode @file http://example.com
Run Code Online (Sandbox Code Playgroud)

如果您的文件包含类似以下内容:

00:0f:1f:64:7d:ff
00:0f:1f:64:7d:ff
00:0f:1f:64:7d:ff
Run Code Online (Sandbox Code Playgroud)

这将导致收到类似以下内容的 POST 请求:

POST / HTTP/1.1
User-Agent: curl/7.19.5 (i486-pc-linux-gnu) libcurl/7.19.5 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.15
Host: example.com
Accept: */*
Content-Length: 90
Content-Type: application/x-www-form-urlencoded

00%3A0f%3A1f%3A64%3A7d%3Aff%0A00%3A0f%3A1f%3A64%3A7d%3Aff%0A00%3A0f%3A1f%3A64%3A7d%3Aff%0A
Run Code Online (Sandbox Code Playgroud)

如果您想添加名称,可以使用多部分形式编码,例如:

curl -F MACS=@file http://example.com
Run Code Online (Sandbox Code Playgroud)

或者

curl -F MACS=<file http://example.com
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您想在参数处发送 URL 编码字符串,而不使用多部分表单编码,则应使用 `curl --data-urlencode param@file http://example.com` 另请参阅 http://superuser。 com/questions/543762/posting-a-files-contents-with-curl (6认同)