Support Questions

Find answers, ask questions, and share your expertise

variable not taking value inside rest api

avatar

curl -iv -u $Ambariuser:$ambaripass -H "X-Requested-By: ambari" -X POST -d '{"Users/user_name":"$username","Users/password":"$password","Users/active":"true","Users/admin":"false"}' http://$localhost:8080/api/v1/users

I am executing this curl command to create user, but $username does not replace what I am passing instead it is passing as $username only, and user been created as $username

1 ACCEPTED SOLUTION

avatar
Master Mentor

@Anurag Mishra

You should use extra single quotes as "'${password}'" and "'${username}'"

Notice:

"'${password}'"

And

"'${username}'"



Example:

# export username=test112233
# export password=test112233_password
# export Ambariuser=admin
# export ambaripass=admin

# curl -iv -u $Ambariuser:$ambaripass -H "X-Requested-By: ambari" -X POST  -d '{"Users/user_name":"'${username}'","Users/password":"'${password}'","Users/active":"true","Users/admin":"false"}' http://localhost:8080/api/v1/users

.

Output:

38467-curl-user-creation.png

.

View solution in original post

7 REPLIES 7

avatar

@Jay SenSharma pls have a look

avatar
Master Mentor

@Anurag Mishra

Before running the actual Curl command please first check if the values are being substituted properly or not?

You can do so by just putting an "echo" before the curl command as following:

# echo "curl -iv -u $Ambariuser:$ambaripass -H "X-Requested-By: ambari" -X POST -d '{"Users/user_name":"$username","Users/password":"$password","Users/active":"true","Users/admin":"false"}' http://$localhost:8080/api/v1/users"

.

avatar
Master Mentor

@Anurag Mishra

Please export the variables properly as following, I see that it is working fine at my end as following:

[root@sandbox ~]# export username=test112233
[root@sandbox ~]# export password=test112233_password
[root@sandbox ~]# export Ambariuser=admin
[root@sandbox ~]# export ambaripass=admin
[root@sandbox ~]# 
[root@sandbox ~]# echo "curl -iv -u $Ambariuser:$ambaripass -H "X-Requested-By: ambari" -X POST -d '{"Users/user_name":"$username","Users/password":"$password","Users/active":"true","Users/admin":"false"}' http://localhost:8080/api/v1/users"

.

Example Output:

[root@sandbox ~]# curl -iv -u $Ambariuser:$ambaripass -H "X-Requested-By: ambari" -X POST -d '{"Users/user_name":"$username","Users/password":"$password","Users/active":"true","Users/admin":"false"}' http://localhost:8080/api/v1/users
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
* Server auth using Basic with user 'admin'
> POST /api/v1/users HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4=
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:8080
> Accept: */*
> X-Requested-By: ambari
> Content-Length: 104
> Content-Type: application/x-www-form-urlencoded
> 
< HTTP/1.1 201 Created
HTTP/1.1 201 Created
< X-Frame-Options: DENY
X-Frame-Options: DENY
< X-XSS-Protection: 1; mode=block
X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< Cache-Control: no-store
Cache-Control: no-store
< Pragma: no-cache
Pragma: no-cache
< Set-Cookie: AMBARISESSIONID=prd2vhf93mq517gq7ashfb9zw;Path=/;HttpOnly
Set-Cookie: AMBARISESSIONID=prd2vhf93mq517gq7ashfb9zw;Path=/;HttpOnly
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
< User: admin
User: admin
< Content-Type: text/plain
Content-Type: text/plain
< Content-Length: 0
Content-Length: 0
< Server: Jetty(8.1.19.v20160209)
Server: Jetty(8.1.19.v20160209)

< 
* Connection #0 to host localhost left intact
* Closing connection #0

.

avatar
Master Mentor

@Anurag Mishra

Now i see what do you mean by "$username" is not being replaced.

34596-username-issue.png

.

This is happening because the Environment variable properties substitution does not happen inside RAW string that is palced inside single quotes like as this is part of the Data String.

-d '{"Users/user_name":"$username","Users/password":"$password","Users/active":"true","Users/admin":"false"}'

.

Please see the difference: (removed the single quote here)

[root@sandbox ~]# echo '{"Users/user_name":"$username","Users/password":"$password","Users/active":"true","Users/admin":"false"}'
{"Users/user_name":"$username","Users/password":"$password","Users/active":"true","Users/admin":"false"}


[root@sandbox ~]# echo {"Users/user_name":"$username","Users/password":"$password","Users/active":"true","Users/admin":"false"}
Users/user_name:test112233 Users/password:test112233_password Users/active:true Users/admin:false

.

avatar

@Jay SenSharmabut if i am removing single quote curl command is not working, not able to create user then

avatar

@Jay SenSharmawhat i can do then, since I have to pass username dynamically, is there other way to achieve this ?

avatar
Master Mentor

@Anurag Mishra

You should use extra single quotes as "'${password}'" and "'${username}'"

Notice:

"'${password}'"

And

"'${username}'"



Example:

# export username=test112233
# export password=test112233_password
# export Ambariuser=admin
# export ambaripass=admin

# curl -iv -u $Ambariuser:$ambaripass -H "X-Requested-By: ambari" -X POST  -d '{"Users/user_name":"'${username}'","Users/password":"'${password}'","Users/active":"true","Users/admin":"false"}' http://localhost:8080/api/v1/users

.

Output:

38467-curl-user-creation.png

.