Vraj Bharambe
Vraj Bharambe

Reputation: 11

How to send a post request with the result of a command or a script using curl?

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.

Upvotes: -1

Views: 5411

Answers (3)

Misunderstood
Misunderstood

Reputation: 5663

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'];

Upvotes: 0

Vraj Bharambe
Vraj Bharambe

Reputation: 11

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.

Upvotes: 1

erik258
erik258

Reputation: 16302

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"
}

Upvotes: 1

Related Questions