Unix cURL POST to specific variable using contents from a file
I've searched for this answer but not fou开发者_运维百科nd 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.
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).
I've seen other forms of curl mentioned on this site, using --url-encode
which says it is not a valid option for curl on my system...
How do you POST the contents of a file as a value to a specific key in a POST using UNIX cURL?
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 --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 / 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
精彩评论