44

I have a curl command:

curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/${job_id}'

The variable job_id has a value in it, say, 1160. When I execute the curl command in shell it gives me the following error:

{"message":"Sorry. An unexpected error occured.", "stacktrace":"Bad Request. The request could not be understood by the server due to malformed syntax."}

If I pass the number '1160' directly in the command, as shown below, the curl command works.

curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/1160'

I want to be able to pass the value of the variable in the curl command.

halfer
  • 19,471
  • 17
  • 87
  • 173
user1452759
  • 7,786
  • 13
  • 39
  • 54

4 Answers4

59

When using variables in , you can only use doubles quotes, not single quotes : the variables inside single quotes are not expanded. Learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words

Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
  • Double quotes while assigning the value 1160 to job_id? You mean like this? "job_id=1160". And then call it inside the curl command using ${job_id}? – user1452759 Nov 12 '12 at 10:26
  • 9
    yes : `job_id=1160; curl -u ${USER_ID}:${PASSWORD} -X GET "http://lppma670.gso.aexp.com:8080/rest/job-execution/job-details/${job_id}"` – Gilles Quenot Nov 12 '12 at 10:28
  • Thank you very much for your help!! It works! :) I will be able to accept your answer in 5 mins. So will do that :) – user1452759 Nov 12 '12 at 10:31
29

I ran into this problem with passing as well, it was solved by using ' " $1 " '

See connection.uri below

curl -X POST -H "Content-Type: application/json" --data '
  {"name": "mysql-atlas-sink",
   "config": {
     "connector.class":"com.mongodb.kafka.connect.MongoSinkConnector",
     "tasks.max":"1",
     "topics":"mysqlstock.Stocks.StockData",
     "connection.uri":"'"$1"'",
     "database":"Stocks",
     "collection":"StockData",
     "key.converter":"io.confluent.connect.avro.AvroConverter",
     "key.converter.schema.registry.url":"http://schema-registry:8081",
     "value.converter":"io.confluent.connect.avro.AvroConverter",
     "value.converter.schema.registry.url":"http://schema-registry:8081",
     "transforms": "ExtractField",
     "transforms.ExtractField.type":"org.apache.kafka.connect.transforms.ExtractField$Value",
     "transforms.ExtractField.field":"after"
}}' http://localhost:8083/connectors -w "\n"
kat1330
  • 4,826
  • 7
  • 36
  • 58
Robert Walters
  • 1,241
  • 13
  • 10
  • 1
    I found that this approach works, but do not believe there is a need for supplemental double-quotes surrounding the environment variable. In other words, in the above example, `'$1'` works just as well as `"'"$1"'"`. I believe both of these approaches work by breaking the single-quoted string into two single-quoted substrings, each adjacent to the expanded `$1` environment variable. – CODE-REaD Jul 06 '20 at 17:28
4

How to pass to with variable(s):

myvar=foobar
curl -H "Content-Type: application/json" --data @/dev/stdin<<EOF
{ "xkey": "$myvar" }
EOF

With the switch -d or --data, the POST request is implicit

Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
1
userdetails="$username:$apppassword"
base_url_part='https://api.XXX.org/2.0/repositories'
path="/$teamName/$repoName/downloads/$filename"
base_url="$base_url_part$path"**strong text**
curl  -L -u "$userdetails" "$base_url" -o "$downloadfilename"
Manohar P
  • 11
  • 1