0

Fail to pass data in Curl POST command.

Code:

printf "\nGet given User details :\n"
curl -s -b ${COOKIE_FILE} -c ${COOKIE_FILE} 'https://api.xxxx.xxxx.com/sso/user?email='${USER_EMAIL} |python -m json.tool > output.txt
user_id=$(cat output.txt | grep -w "id" | awk -F ':' {'print $2'} | sed -e 's|[," ]||g');
echo "User ID for given add User: $user_id"

printf "\nGet given Role/Group details :\n"
curl -s -b ${COOKIE_FILE} -c ${COOKIE_FILE} 'https://api.xxxx.xxxx.com/role?name='${ROLE_NAME} | python -m json.tool > output_roles.txt
role_id=$(cat output_roles.txt | grep -w "id" | awk -F ':' {'print $2'} | sed -e 's|[," ]||g');

printf "\nAdding User to given Group :\n"
curl -s -b ${COOKIE_FILE} -c ${COOKIE_FILE} -X POST -data '{"role_users":"$role_id","user_roles":"$user_id"}' 'https://api.xxx.xxxx.com/rolesusers'

Curl POST Command is not working for me. Its not give me any error and no changes I see on application.

Curl command is working when I pass hard coded values.

curl -s -b ${COOKIE_FILE} -c ${COOKIE_FILE} -X POST -H "Accept: Application/json" -H "Content-Type: application/json" --data '{"role_users":22, "user_roles":99}' 'https://api.xxx.xxxx.com/rolesusers'
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56

1 Answers1

0

Got it - by adding '

curl -s -b ${COOKIE_FILE} -c ${COOKIE_FILE} -X POST -H "Accept: Application/json" -H "Content-Type: application/json" --data '{"role_users":"'$role_id'", "user_roles":"'$user_id'"}' 'https://api.xxx.xxxx.com/rolesusers' > output_add.txt
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
  • 1
    That leaves the variable in an unquoted section, which can cause trouble. It's better to add double-quotes around it, like this: `'..."user_roles":"'"$user_id"'"}' ...`. Yes, this is confusing; you're mixing multiple types and levels of quotes. – Gordon Davisson Mar 30 '20 at 05:15