bash Unix cURL POST 使用文件中的内容到特定变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7489453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unix cURL POST to specific variable using contents from a file
提问by Rimer
I've searched for this answer but not found anything that works or that completely matches my problem.
我已经搜索了这个答案,但没有找到任何有效或完全符合我的问题的答案。
Using Unix cURL, I need to POST a key/val pair to a server. The key will be "MACs", and the contents of a file of newline separated MAC addresses will be the VALUE for this POST.
使用 Unix cURL,我需要将密钥/值对发布到服务器。密钥将是“MACs”,换行符分隔的 MAC 地址文件的内容将是此 POST 的 VALUE。
I've tried:
我试过了:
curl -d @filename http://targetaddress.com, but when the target receives the incoming POST, the POST var is empty. (PHP is receiving).
curl -d @filename http://targetaddress.com,但是当目标收到传入的 POST 时,POST 变量为空。(PHP 正在接收)。
I've seen other forms of curl mentioned on this site, using --url-encodewhich says it is not a valid option for curl on my system...
我在本网站上看到过其他形式的 curl,使用--url-encode它表示它不是我系统上 curl 的有效选项......
How do you POST the contents of a file as a value to a specific key in a POST using UNIX cURL?
如何使用 UNIX cURL 将文件内容作为值发布到 POST 中的特定键?
采纳答案by bohica
According to curl man page -d is the same as --data-ascii. To post the data as binary use --data-binary and to post with url encoding use --data-urlencode. So as your file is not URL encoded if you want to send it URL encoded use:
根据 curl 手册页 -d 与 --data-ascii 相同。将数据发布为二进制使用 --data-binary 并使用 url 编码发布使用 --data-urlencode。因此,如果您想发送 URL 编码的文件,那么您的文件不是 URL 编码的,请使用:
curl --data-urlencode @file http://example.com
If you file contains something like:
如果您的文件包含以下内容:
00:0f:1f:64:7d:ff
00:0f:1f:64:7d:ff
00:0f:1f:64:7d:ff
this will result in a POST request received something like:
这将导致收到类似以下内容的 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
If you want to add a name you can use multipart form encoding something like:
如果要添加名称,可以使用多部分形式编码,例如:
curl -F MACS=@file http://example.com
or
或者
curl -F MACS=<file http://example.com

