1

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 $URL and $HEADERS are from a .conf and holds constant values:
URL="localhost:8080/employees"
HEADERS="Content-type:application/json"
  • The $data variable is built when the main bash runs an holds random values for the keys, like this:
{ "age":"59", "firstName":"Lauree", "lastName":"Inna", "role":"Mage" }
0

1 Answer 1

2

You shouldn't have \' around your variables. That will put a literal single quote into that argument.

Use double quotes to make a variable expand as a single argument.

curl -X POST "$URL" -H "$HEADERS" -d "$data"
Sign up to request clarification or add additional context in comments.

2 Comments

How are you setting $HEADERS? With source filename.conf?
actually I did what you said and now it worked. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.