I have a main bash script that I want to use to make a HTTP request using curl.
The line where curl command is executed is like this:
echo $(curl -X POST $URL -H \'$HEADERS\' -d \'$data\')
When this line runs I get an error:
"status":415, "error":"Unsupported Media Type", "message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
But if I execute the same line without the $() operator, like this:
echo curl -X POST $URL -H \'$HEADERS\' -d \'$data\'
I'll get something like this as output:
curl -X POST localhost:8080/employees -H 'Content-type:application/json' -d '{ "age":"25", "firstName":"Peggy", "lastName":"Ailbert", "role":"Thief" }'
That runs perfecty if copied to terminal.
I would like to know what is going wrong with the code.
NOTES:
- The variables
$URLand$HEADERSare from a.confand holds constant values:
URL="localhost:8080/employees"
HEADERS="Content-type:application/json"
- The
$datavariable is built when the main bash runs an holds random values for the keys, like this:
{ "age":"59", "firstName":"Lauree", "lastName":"Inna", "role":"Mage" }
I've tried the answer in this question: Curl command doesn't work in bash script but it didn't worked for me.
The source for the bash script can be found at https://github.com/JeanCHilger/automated-requester/blob/master/post/POST.sh (the 70th line is where I am getting the error)