Home > Back-end >  How to send a post request with the result of a command or a script using curl?
How to send a post request with the result of a command or a script using curl?

Time:09-02

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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related