6

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.

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?

Rimer
  • 2,054
  • 6
  • 28
  • 43
  • If you're using unix command-line, then you're using a shell as well. It will improve your chances to get a useful answer by adding a tag for bash or ksh or ??? Good luck. – shellter Sep 20 '11 at 18:08

1 Answers1

4

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
bohica
  • 5,932
  • 3
  • 23
  • 28
  • 5
    Note that if you want to send a URL encoded string at a parameter, WITHOUT using multipart form encoding, you should use `curl --data-urlencode param@file http://example.com` See also http://superuser.com/questions/543762/posting-a-files-contents-with-curl – Edward Z. Yang Jan 28 '17 at 07:20