I am trying to write a script which will execute two curl requests in bash. This is my code:
#!/bin/bash
ipadd="192.168.1.1"
start_url="http://$ipadd/startPlayer"
stop_url="http://$ipadd/stopPlayer"
header1="Accept: application/json"
header2="Content-Type: application/json"
stp="28508ab5-9591-47ed-9445-d5e8e9bafff6"
function start_player {
curl --verbose -H \"${header1}\" -H \"${header2}\" -X PUT -d '{\"id\": \"$stp\"}' ${start_url}
}
function stop_player {
curl -X PUT $stop_url
}
stop_player
start_player
The stop_player function works without a problem, but the first function doesn't work. I just want to execute the following CURL request: curl --verbose -H "Accept: application/json" -H "Content-Type: application/json" -X PUT -d '{"id": "c67664db-bef7-4f3e-903f-0be43cb1e8f6"}' http://192.168.1.1/startPlayer
If I echo the start_player function, the output is exactly as it should be, but if I execute the start_player function I got an error: Could not resolve host: application
. I think it is because bash is splitting the headers but why it works fine with echo, but not in bash?