52

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,824
  • 17
  • 99
  • 186
user1452759
  • 8,810
  • 15
  • 42
  • 58

5 Answers5

62

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 Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 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
  • 11
    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 Quénot 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
37

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
  • 5,134
  • 7
  • 38
  • 61
Robert Walters
  • 1,367
  • 14
  • 10
  • 2
    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 Quénot
  • 173,512
  • 41
  • 224
  • 223
3

use variable in a double-quote single-quote "' $variable '"

#!/usr/bin/bash
token=xxxxxx
curl --location --request POST 'http://127.0.0.1:8009/submit/expense/' \
        --form 'token="'$token'"' \
        --form 'text="'$1'"' \
        --form 'amount="'$2'"'
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
  • 111
  • 1