HELP - loop a curl command with different variables from input file

Hi guys!

Kind of new to bash scripting and now I'm stuck.

I need to curl with these variables:

"{ \"nodename\": \"$1\", \"ipaddress\": \"$2\", \"poolname\": \"$3\", \"port\": \"$4\", \"loadbalancer\" : \"$5\" }"

and my input_file.txt contains

server001 10.10.10.01 serverpool1 80 loadbalancer01
server002 10.10.10.02 serverpool1 80 loadbalancer01
server003 10.10.10.03 serverpool1 80 loadbalancer01

Can't figure out how to "insert" the variables in the input_file.txt to the curl :confused:

Thanks in advance :smiley:

Welcome to the forum.

Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

I'm pretty sure you won't " curl with these variables:", so please provide complete commands so people don't need to guess what you're doing or want to do. To read, expand, and use data from files, try

while read A B C D E; do echo "{ \"nodename\": \"$A\", \"ipaddress\": \"$B\", \"poolname\": \"$C\", \"port\": \"$D\", \"loadbalancer\" : \"$E\" }"; done < input_file.txt
{ "nodename": "server001", "ipaddress": "10.10.10.01", "poolname": "serverpool1", "port": "80", "loadbalancer" : "loadbalancer01" }
{ "nodename": "server002", "ipaddress": "10.10.10.02", "poolname": "serverpool1", "port": "80", "loadbalancer" : "loadbalancer01" }
{ "nodename": "server003", "ipaddress": "10.10.10.03", "poolname": "serverpool1", "port": "80", "loadbalancer" : "loadbalancer01" }
1 Like

Hi @RudiC. Thank you for the reply and apologies for the lack of pertinent info.

You are absolutely correct that I won't "curlwith these variables" so let me try to rephrase.

Curl will be used for an API call to add multiple nodes to a load balancer pool (hence my cry for help on how to go about reading, expanding and using a source file) and the variables above are all needed.
Looks something like this -

curl -u 'usernamepassword' "Content-Type: application/json" POST https://api/add/ "{ \"nodename\": \"$A\", \"ipaddress\": \"$B\", \"poolname\": \"$C\", \"port\": \"$D\", \"loadbalancer\" : \"$E\" }"

I will be running a bash script to loop the API call from a RHEL 6.5 bastion host.

Thank you again and I'll try what you suggested :b:

OK so the read and expand from file is now good but here is another problem:

In my script the curl command that should reach the server is -

curl -u 'user:password' -H "Content-Type: application/json" -X POST //api/add/ -d '{ "nodename": "server001", "address": "10.10.10.01", "poolname": "serverpool1", "port": "80", "lbname" : "loadbalancer01" }'

but this is what is sent -
curl -u userpassword -H 'Content-Type: application/json' -X POST //api/add/ -d '{ "nodename": "server001", "address": "10.10.10.01", "poolname": "serverpool1", "port": "80", "lbname" : "loadbalancer01" }'

I've tried different ways for escape like \ ` and other combinations of but it's just not working.

Thanks again for any help on this.

Without the command that you issue in the context that it's used it's not that easy to analyse what's going wrong.