14

I want to send the output of uptime and df commands to a web app using cURL. I tried:

uptime | curl http://someurl.com -T -

But that didn't seem to send the data.

What is the proper way to send the output of a command to cURL as a POST parameter? (or wget if that's much easier)

quanta
  • 50,327
  • 19
  • 152
  • 213
Callmeed
  • 2,705
  • 4
  • 18
  • 15
  • did you attempt to use the redirector for stdout `>` not a pipe `|`? Also take a look at `xargs`. – mbrownnyc Sep 20 '11 at 20:07
  • When you say "as a POST parameter", do you mean you need it to be as if you typed in your uptime in a field (with a name) on a form and pressed submit, or as if you saved your uptime to a file, then used a file upload field to select the file, then pressed submit? These are two different ways of POSTing data. `-T` with `http[s]://` uses PUT, not POST. – DerfK Sep 20 '11 at 20:50
  • @DerfK Yes, I'd like it to be as if I pasted the uptime results into a textarea field and it was POSTed to a URL. – Callmeed Sep 20 '11 at 21:03
  • Look this is described in the manual of curl: http://curl.haxx.se/docs/manpage.html#-d Or in the manual of wget (under `--post-data=string`): http://www.gnu.org/software/wget/manual/wget.html#HTTP-Options – mailq Sep 20 '11 at 20:15

1 Answers1

33

You can use the -d option in curl with a @- argument to accept input from a pipe. You will need to construct the key-value pairs yourself. Try this:

echo "time=`uptime`" | curl -d @- http://URL

The backticks (`) denote that the enclosed command (in this case uptime) should be executed, and the backtick-quoted text replaced with the output of the executed command.

saffsd
  • 483
  • 5
  • 8
  • 4
    If the output of the command includes linebreaks (like `df`) you might want to use `curl --data-binary @- URL` or in newer versions of curl: `uptime | curl --data-urlencode time@- URL` – xebeche Apr 02 '13 at 18:51