Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix cURL POST to specific variable using contents from a file

Tags:

post

bash

unix

curl

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?

like image 500
Rimer Avatar asked Sep 20 '11 17:09

Rimer


People also ask

How do you pass file content in curl command?

Send the data using curl's –d or –data option. To execute a CURL file upload, you need to use the –d command-line option and begin data with the @ symbol. The file's name should come after the data with @ symbol so CURL can read it and send it to the server.

Can curl read from file?

The path to the file needs to be preceded by an @ symbol, so curl knows to read from a file.

How do I specify Content-Type in curl?

To send the Content-Type header using Curl, you need to use the -H command-line option. For example, you can use the -H "Content-Type: application/json" command-line parameter for JSON data. Data is passed to Curl using the -d command-line option.

How do I upload a file using curl post?

To post a file with Curl, use the -d or -F command-line options and start the data with the @ symbol followed by the file name. To send multiple files, repeat the -F option several times.


1 Answers

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
like image 96
bohica Avatar answered Oct 19 '22 09:10

bohica