44

我有一个 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 命令中传递变量的值。

4

4 回答 4

59

中使用变量时,只能使用双引号,不能使用单引号:单引号内的变量不展开。了解 ' 和 " 和 ` 之间的区别。参见http://mywiki.wooledge.org/Quoteshttp://wiki.bash-hackers.org/syntax/words

于 2012-11-12T10:23:11.700 回答
26

我在传递时也遇到了这个问题,它是通过使用'“$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"
于 2020-01-30T18:02:12.303 回答
4

如何使用传递给

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

使用 switch-d--dataPOST请求是隐式的

于 2021-06-04T19:57:15.140 回答
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"
于 2020-10-01T18:38:17.530 回答