Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH - Split CURL command into multiple lines

Tags:

bash

curl

I have a working curl command that I'd like to split out to make it easier to read.

curl d "valuea=1234&valueb=4567&valuec=87979&submit=Set" -XPOST "http://$ipa/school.cgi" > /dev/null

I've tried several ways to do this, but none seems to work.

curl -d "valuea=1234"\
     -d "valueb=4567"\
     -d "valuec=87979"\
     -d "submit=Set"\
 -XPOST "http://$ipa/school.cgi"

curl -d "valuea=1234\
         valueb=4567\
         valuec=87979\
         submit=Set"\
 -XPOST "http://$ipa/school.cgi"

Can someone advise how to do it ?

Thanks

like image 342
Tom Avatar asked Oct 26 '25 14:10

Tom


1 Answers

The first approach is right, I experienced problems with spreading commands on multiple lines on some environments which were trimming whitespaces, hence it's a good idea to add a space before the backslashes:

curl -d "valuea=1234" \
     -d "valueb=4567" \
     -d "valuec=87979" \
     -d "submit=Set" \
  -XPOST "http://$ipa/school.cgi"

Eventually try it against a simple service which will inform you on what's receiving, like this one:

// echorequest.js
const http = require('http');

const hostname = '0.0.0.0';
const port = 3001;

const server = http.createServer((req, res) => {
  console.log(`\n${req.method} ${req.url}`);
  console.log(req.headers);

  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');

  let data = '';

  req.on('data', function(chunk) {
    data += chunk
  });

  req.on('end', function() {
    console.log('BODY: ' + data);
    res.end(data + "\n");
  });
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

... run by node echorequest.js (& change target of the command: -XPOST "localhost:3001")

like image 127
Tomas Varga Avatar answered Oct 29 '25 07:10

Tomas Varga