0

I have the following small bash script:

TEST=$(curl -X POST -s -H "Content-type: application/json" -X POST http://$SITE/REST/v1/ -d '
{
    "Authorization":"Bearer '$TOKEN'"
}
' --trace-ascii /dev/stdout)

In this case, $TOKEN=c4cc0c81e279defd4bd261e9e1550e8a1ded5JKg5

However, when I send the request with --trace-ascii enabled, I can see the following in the output:

{. "Authorization":"Bearer "c4cc0c81e279defd4bd261e9 0040: e1550e8a1ded5JKg5"". }

Any reason why this gets split up? How can I prevent this?

5
  • Maybe the variable TOKEN is already set to the truncated value? Commented Dec 21, 2017 at 14:01
  • I am seeing the same issue when I use Bearer c4cc0c81e279defd4bd261e9e1550e8a1ded5JKg5 in plain text. It also gets cut off Commented Dec 21, 2017 at 14:14
  • i think the problem is on the ' '$TOKEN' ' in this way the only word non single quoted is $TOKEN Commented Dec 21, 2017 at 14:17
  • Maybe this behaviour is due to shell quoting rules. Try to save your post data into a file and specify it on the command line -d @post.json Commented Dec 21, 2017 at 15:36
  • Count your single quotes. Do you actually want single quotes embedded in the data you post? Commented Dec 21, 2017 at 17:12

1 Answer 1

1

I would do this, which will help with the quoting:

data=$(printf '{"Authorization":"Bearer %s"}' "$TOKEN")
output=$(curl -s -H "Content-type: application/json" -X POST http://$SITE/REST/v1/ -d "$data" --trace-ascii /dev/stdout)

I suspect you may need to send Authorization as a header not as data. Try this, it's quite readable

curl_opts=(
    --silent
    --header "Content-type: application/json"
    --header "Authorization: Bearer $token"
    --trace-ascii -
)
curl "${curl_opts[@]}" -X POST -d "$other_data" "$url"

Note you should get out of the habit of using ALL_CAPS_VARNAMES: leave those as reserved for the shell.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! How would the data printf part look like if I would need to pass two key-value?
At a bash prompt, enter help printf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.