1

My script deals with browser automation using cURL. I need to POST user input username. I can do this by hard-coding the username like this:

# sid -> session Id
# eid -> element Id (input box here)

curl -d '{"value":["username"]}' http://localhost:9515/session/$sid/element/$eid/value

This successfully posts username but I want to read username from user and pass this to value.

I tried:

read userName
curl -d '{"value":[$userName]}' http://localhost:9515/session/$sid/element/$eid/value

This gives me "missing command parameter" error. Passing only $userName instead of [$userName] gives "invalid argument: 'value' must be a list"

How do I pass a variable (userName) to the curl POST request in this case?

Adarsh TS
  • 193
  • 15
  • 1
    (1) The single quotes block parameter expansion to occur; and (2) Why do you now have brackets around your user name, and in the original version of the command you didn't? – user1934428 Mar 18 '21 at 10:09
  • @user1934428, I don't get what you are saying in (1). I've made the changes as pointed out in (2). Thanks. – Adarsh TS Mar 18 '21 at 15:00
  • 1
    If you have single quotes, no parameter expansion occurs inside. Hence, `' ... $userName'` does not expand `userName. – user1934428 Mar 19 '21 at 07:29

1 Answers1

1

It looks like you've tried to use the variable within single quotes. Try to use double quotes instead:

read userName
curl -d "{\"value\":\"${userName}\"}" http://localhost:9515/session/$sid/element/$eid/value

Explanation:

Single quotes (' ') operate similarly to double quotes, but do not permit referencing variables, since the special meaning of $ is turned off. Within single quotes, every special character except ' gets interpreted literally. Consider single quotes ("full quoting") to be a stricter method of quoting than double quotes ("partial quoting").

Taken from: https://tldp.org/LDP/abs/html/quotingvar.html

Oliniusz
  • 413
  • 3
  • 8