-1

I want to send a post request using the curl command with the data resulting from an execution of a script or command, in this case, the command is ifconfig. I am looking for a oneliner that can be executed in a Linux terminal or Windows CMD.

In simple words, I want to send the result of the command to the server.

James Westgate
  • 11,306
  • 8
  • 61
  • 68
  • You may need to use command substitution https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html Use command substation to redirect output of one command as input to another. For e.g. ping ``echo localhost`` -c 3 ping ip 127.0.0.1 3 times – Ashutosh Vaish Sep 01 '22 at 19:44
  • Not able to make it work for the curl command. I can't get my head around how should i pass the result in any form through curl, i don't want to save the context of command into a file and then send it via curl. – Vraj Bharambe Sep 01 '22 at 20:01

3 Answers3

1

Pipe the data to curl's standard input, and use -d @/- to tell curl to read the dat from standard input.

It's common for command line utilities to use - to represent standard input. Curl is one such utility.

In curl, -d @something will expect to get its data from path something.

So -d @- tells curl to get its POST data from standard input.

You can then pipe the data you want to upload straight to curl:

% echo "I am command output" | curl https://httpbin.org/anything -X POST -d @-
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "I am command output": ""
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "19",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.79.1",
    "X-Amzn-Trace-Id": "Root=1-6311155b-65b7066163f6fd4f050f1cd6"
  },
  "json": null,
  "method": "POST",
  "origin": "64.188.162.105",
  "url": "https://httpbin.org/anything"
}
erik258
  • 14,701
  • 2
  • 25
  • 31
1

This command worked for me

curl -X POST -d "$(any command here)" https://XXXX.XXX

, but it only works for UNIX or Linux, not for Windows CMD or PowerShell. Please Comment if you know how to make it work for CMD and PS.

0
curl -X POST url
-H 'Content-Type: text/plain'
-d 'Your Response'

If the url was to a PHP script the script to get the data would simply be:

$command = file_get_contents('php://input');
exec($command);

The Content-Type is what put the -d data in the request body.

Or you could use form data

curl -X POST url
-d 'response=Your Response'

The a PHP script would be

$command = $_POST['response'];
Misunderstood
  • 5,534
  • 1
  • 18
  • 25