如何在shell脚本中的curl命令中传递变量

fwzugrvs  于 2022-11-13  发布在  Shell
关注(0)|答案(5)|浏览(449)

我有一个curl命令:

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

变量job_id中有一个值,比如说1160。当我在shell中执行curl命令时,它给出了以下错误:

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

如果我在命令中直接传递数字'1160',如下所示,curl命令就可以工作。

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

我希望能够在curl命令中传递变量的值。

k97glaaz

k97glaaz1#

shell中使用变量时,只能使用双引号,而不能使用单引号:单引号内的变量没有展开。了解' and“和'之间的区别。请参阅http://mywiki.wooledge.org/Quoteshttp://wiki.bash-hackers.org/syntax/words

i7uq4tfw

i7uq4tfw2#

我在传球时也遇到了这个问题,通过使用“$1“解决了这个问题
请参阅下面的connection.uri

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

x7yiwoj43#

如何使用shell变量将json传递到curl

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

使用开关-d--data时,POST请求是 * 隐式的 *

6ie5vjzr

6ie5vjzr4#

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

ymzxtsji5#

在双引号和单引号中使用变量“' $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'"'

相关问题