user1452759
user1452759

Reputation: 9430

How to pass a variable in a curl command in shell scripting

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.

Upvotes: 58

Views: 161636

Answers (7)

Kiran
Kiran

Reputation: 71

Just in case, someone is still looking for the right approach, this worked for me

curl 'https://api.xyz.com/client/v4/accounts/'$XYZ_ACCOUNT_ID'/access/apps' \
    --header "X-Auth-Email: $EMAIL_ADDRESS" \
    --header "X-Auth-Key: $XYZ_API_KEY"

Either https://api.xyz.com/client/v4/accounts/"'$XYZ_ACCOUNT_ID'"/access/apps pr https://api.xyz.com/client/v4/accounts/"$XYZ_ACCOUNT_ID"/access/apps didn't work. Double-quoting or single-quoting variables in Headers also didn't work.

Upvotes: 0

Jeff Spicoli
Jeff Spicoli

Reputation: 500

Locally on a Mac storing a variable (PERSONAL_TOKEN) within the ~/.zshrc file where the token value is a string:

# add variable within ~/.zshrc
export PERSONAL_TOKEN=string-of-sensitive-token

Restart the terminal or exec zshrc to have variable changes take effect, can confirm variable is set with env | grep PERSONAL_TOKEN

cURL statement with variable signaled by dollar sign within double quotes ("$PERSONAL_TOKEN"):

# run curl command within terminal (shell) environment
curl --request GET --url "address_for.curl.request" \
  --header "Authorization: Basic $PERSONAL_TOKEN"

Note:

  • single quotes do not insert variable name
  • adding inside existing double quotes, string does not need to be escaped
  • variable is also inserted without quotations example: curl -v $URL_ADDRESS

Upvotes: 0

Gilles Quénot
Gilles Quénot

Reputation: 184955

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 https://web.archive.org/web/20230314111401/https://wiki.bash-hackers.org/syntax/words

Upvotes: 68

Mohammad reza Saberi
Mohammad reza Saberi

Reputation: 87

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'"'

Upvotes: 4

Gilles Quénot
Gilles Quénot

Reputation: 184955

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

Upvotes: 4

Manohar P
Manohar P

Reputation: 111

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"

Upvotes: 1

Robert Walters
Robert Walters

Reputation: 1397

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"

Upvotes: 37

Related Questions