I'm trying to execute an script in bash, but throw me this error,
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched close brace/bracket in URL position 24:
UTC","user_id":"01234"}}
i tried removing the braces but does not work, this is the line,
response=$(curl -X POST -H "Authorization: Bearer ${bearer_token}" -H "Content-Type: application/json" -d '{"cursus_user":{"begin_at":"'${start}'","cursus_id":"'${cursus_id}'","end_at":"'${end}'","user_id":"'${user}'"}}' "https://xxxxxx/xxxxxx.com")
Anyone know where is the issue?, i'm stuck, thanks in advance.
[ UPDATE ]
Really i can't see the error :(
'
{
"cursus_user":
{
"begin_at": "'${start}'",
"cursus_id": "'${cursus_id}'",
"end_at": "'${end}'",
"user_id": "'${user}'"
}
}
'
CodePudding user response:
This is really more of a formatted comment.
Two tips:
- use jq to generate JSON -- it will get all the quoting correct for you
- use arrays for readability (I dislike scrolling horizontally)
data=$(
jq --null-input \
--compact-output \
--arg begin_at "$start" \
--arg cursus_id "$cursus_id" \
--arg end_at "$end" \
--arg user_id "$user" \
'{cursus_user: $ARGS.named}'
)
curl_opts=(
-X POST
-H "Authorization: Bearer ${bearer_token}"
-H "Content-Type: application/json"
-d "$data"
)
response=$(curl "${curl_opts[@]}" "https://xxxxxx/xxxxxx.com")
CodePudding user response:
Shellcheck identifies several unquoted variables in the curl
command. It even provides the corrected code:
response=$(curl -X POST -H "Authorization: Bearer ${bearer_token}" -H "Content-Type: application/json" -d '{"cursus_user":{"begin_at":"'"${start}"'","cursus_id":"'"${cursus_id}"'","end_at":"'"${end}"'","user_id":"'"${user}"'"}}' "https://xxxxxx/xxxxxx.com")
Using Shellcheck often saves a lot of time when working with shell code.