Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send a file's contents as a POST parameter using cURL?

I'm trying to use cURL to POST the contents of a file, as if I'd pasted that contents in to an html textarea. That's to say I don't want to upload the file, I just want a post parameter called foo to be filled with text from a file called bar.txt. bar.txt's contents may include newlines, quotes, and so on.

Is this possible?

Thanks.

Edit: I found out how to do it in the end:

curl --data-urlencode "[email protected]" http://example.com/index.php

This will take the contents of the file bar.txt, url encode it, place the resultant string in a parameter called foo in a POST request of http://example.com/index.php.

I can't speak to whether the solutions others have suggested will work or not, but the one above seems like the best way.

like image 510
Duncan Marshall Avatar asked Jan 11 '14 11:01

Duncan Marshall


Video Answer


1 Answers

You can by doing something like:

$ curl --data "foo:$(cat foo.txt)" http://localhost/yourfile.php

Note that you'll probably want to encode the file, as cacheguard said. To encode it in base64, just modify the previous command like this:

$ curl --data "foo:$(cat foo.txt | base64)" http://localhost/yourfile.php
like image 141
rul Avatar answered Nov 15 '22 00:11

rul