225

当我回显时,我得到了这个,当我将它输入终端时运行

curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data '{"account":{"email":"akdgdtk@test.com","screenName":"akdgdtk","type":"NIKE","passwordSettings":{"password":"Starwars1","passwordConfirm":"Starwars1"}},"firstName":"Test","lastName":"User","middleName":"ObiWan","locale":"en_US","registrationSiteId":"520","receiveEmail":"false","dateOfBirth":"1984-12-25","mobileNumber":"9175555555","gender":"male","fuelActivationDate":"2010-10-22","postalCode":"10022","country":"US","city":"Beverton","state":"OR","bio":"This is a test user","jpFirstNameKana":"unsure","jpLastNameKana":"ofthis","height":"80","weight":"175","distanceUnit":"MILES","weightUnit":"POUNDS","heightUnit":"FT/INCHES"}' https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx

但是当在 bash 脚本文件中运行时,我得到了这个错误

curl: (6) Could not resolve host: application; nodename nor servname provided, or not known
curl: (6) Could not resolve host: is; nodename nor servname provided, or not known
curl: (6) Could not resolve host: a; nodename nor servname provided, or not known
curl: (6) Could not resolve host: test; nodename nor servname provided, or not known
curl: (3) [globbing] unmatched close brace/bracket at pos 158

这是文件中的代码

curl -i \
-H '"'Accept: application/json'"' \
-H '"'Content-Type:application/json'"' \
-X POST --data "'"'{"account":{"email":"'$email'","screenName":"'$screenName'","type":"'$theType'","passwordSettings":{"password":"'$password'","passwordConfirm":"'$password'"}},"firstName":"'$firstName'","lastName":"'$lastName'","middleName":"'$middleName'","locale":"'$locale'","registrationSiteId":"'$registrationSiteId'","receiveEmail":"'$receiveEmail'","dateOfBirth":"'$dob'","mobileNumber":"'$mobileNumber'","gender":"'$gender'","fuelActivationDate":"'$fuelActivationDate'","postalCode":"'$postalCode'","country":"'$country'","city":"'$city'","state":"'$state'","bio":"'$bio'","jpFirstNameKana":"'$jpFirstNameKana'","jpLastNameKana":"'$jpLastNameKana'","height":"'$height'","weight":"'$weight'","distanceUnit":"MILES","weightUnit":"POUNDS","heightUnit":"FT/INCHES"}'"'" "https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx"

我认为我的引号有问题,但我玩过很多次,也遇到过类似的错误。所有变量都在实际脚本中用不同的函数定义

4

9 回答 9

338

您无需将包含自定义标头的引号传递给 curl。data此外,应该引用参数中间的变量。

首先,编写一个生成脚本发布数据的函数。这使您免于各种有关 shell 引用的麻烦,并且比在您的尝试中提供 curl 的调用行上的 post 数据更容易阅读维护脚本:

generate_post_data()
{
  cat <<EOF
{
  "account": {
    "email": "$email",
    "screenName": "$screenName",
    "type": "$theType",
    "passwordSettings": {
      "password": "$password",
      "passwordConfirm": "$password"
    }
  },
  "firstName": "$firstName",
  "lastName": "$lastName",
  "middleName": "$middleName",
  "locale": "$locale",
  "registrationSiteId": "$registrationSiteId",
  "receiveEmail": "$receiveEmail",
  "dateOfBirth": "$dob",
  "mobileNumber": "$mobileNumber",
  "gender": "$gender",
  "fuelActivationDate": "$fuelActivationDate",
  "postalCode": "$postalCode",
  "country": "$country",
  "city": "$city",
  "state": "$state",
  "bio": "$bio",
  "jpFirstNameKana": "$jpFirstNameKana",
  "jpLastNameKana": "$jpLastNameKana",
  "height": "$height",
  "weight": "$weight",
  "distanceUnit": "MILES",
  "weightUnit": "POUNDS",
  "heightUnit": "FT/INCHES"
}
EOF
}

然后很容易在 curl 的调用中使用该函数:

curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data "$(generate_post_data)" "https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx"

这就是说,这里有一些关于 shell 引用规则的说明:

参数中的双引号-H(如-H "foo bar")告诉 bash 将里面的内容保留为单个参数(即使它包含空格)。

参数中的单引号--data(如--data 'foo bar')执行相同的操作,除了它们逐字传递所有文本(包括双引号字符和美元符号)。

要在单引号文本中间插入变量,您必须结束单引号,然后与双引号变量连接,然后重新打开单引号以继续文本:'foo bar'"$variable"'more foo'

于 2013-06-10T20:54:57.637 回答
153

使用https://httpbin.org/和内联 bash 脚本测试的解决方案
1.对于其中没有空格的变量,即1
只需在替换所需字符串'之前和之后添加$variable

for i in {1..3}; do \
  curl -X POST -H "Content-Type: application/json" -d \
    '{"number":"'$i'"}' "https://httpbin.org/post"; \
done

2.对于带空格的输入:用附加的ie
包装变量:""el a"

declare -a arr=("el a" "el b" "el c"); for i in "${arr[@]}"; do \
  curl -X POST -H "Content-Type: application/json" -d \
    '{"elem":"'"$i"'"}' "https://httpbin.org/post"; \
done

哇工作:)

于 2017-03-30T10:52:54.167 回答
48

Curl 可以从文件中发布二进制数据,因此每当我需要使用 curl 发布令人讨厌的内容并且仍想访问当前 shell 中的 var 时,我一直在使用进程替换并利用文件描述符。就像是:

curl "http://localhost:8080" \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
--data @<(cat <<EOF
{
  "me": "$USER",
  "something": $(date +%s)
  }
EOF
)

这最终看起来就像--data @/dev/fd/<some number>像普通文件一样被处理。无论如何,如果你想看到它在本地工作,只需nc -l 8080首先运行并在不同的 shell 中触发上述命令。你会看到类似的东西:

POST / HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.43.0
Accept: application/json
Content-Type:application/json
Content-Length: 43

{  "me": "username",  "something": 1465057519  }

如您所见,您可以在 heredoc 中调用 subshel​​ls 和诸如此类的东西以及引用 vars。快乐的黑客希望这有助于'"'"'""""'''""''.

于 2016-06-04T16:37:07.730 回答
12

晚了几年,但如果您使用 eval 或反引号替换,这可能会对某人有所帮助:

postDataJson="{\"guid\":\"$guid\",\"auth_token\":\"$token\"}"

使用 sed 去除响应开头和结尾的引号

$(curl --silent -H "Content-Type: application/json" https://${target_host}/runs/get-work -d ${postDataJson} | sed -e 's/^"//' -e 's/"$//')
于 2018-01-17T03:42:40.070 回答
12

我们可以使用单引号为 curl 分配一个变量,并将一些其他变量'包裹在双单双引号中,以便在curl-variable中进行替换。然后我们可以很容易地使用curl 变量,这里是."'"MERGE

例子:

# other variables ... 
REF_NAME="new-branch";

# variable for curl using single quote => ' not double "
MERGE='{
    "repository": "tmp",
    "command": "git",
    "args": [
        "pull",
        "origin",
        "'"$REF_NAME"'"
    ],
    "options": {
        "cwd": "/home/git/tmp"
    }
}';

注意这一行:

    "'"$REF_NAME"'"

所以我们可以像往常一样使用这个 bash 变量$MERGE并调用curl :

curl -s -X POST localhost:1365/M -H 'Content-Type: application/json' --data "$MERGE" 
于 2021-02-17T17:00:15.073 回答
9

在此处的答案指导之后,这对我来说实际上是有效的:

export BASH_VARIABLE="[1,2,3]"
curl http://localhost:8080/path -d "$(cat <<EOF
{
  "name": $BASH_VARIABLE,
  "something": [
    "value1",
    "value2",
    "value3"
  ]
}
EOF
)" -H 'Content-Type: application/json'
于 2019-09-09T22:47:43.157 回答
5
  • 来自阿托斯爵士的信息完美无缺!

这是我必须在我的 curl 脚本中为 couchDB 使用它的方法。它真的帮了很多忙。谢谢!

bin/curl -X PUT "db_domain_name_:5984/_config/vhosts/$1.couchdb" -d '"/'"$1"'/"' --user "admin:*****"
于 2014-04-07T19:54:03.460 回答
3

现有答案指出 curl 可以从文件中发布数据,并使用 heredocs 来避免过多的引号转义并清楚地将 JSON 分解为新行。但是,不需要定义函数或从 cat 捕获输出,因为 curl 可以从标准输入发布数据。我发现这种形式非常易读:

curl -X POST -H 'Content-Type:application/json' --data '$@-' ${API_URL} << EOF
{
  "account": {
    "email": "$email",
    "screenName": "$screenName",
    "type": "$theType",
    "passwordSettings": {
      "password": "$password",
      "passwordConfirm": "$password"
    }
  },
  "firstName": "$firstName",
  "lastName": "$lastName",
  "middleName": "$middleName",
  "locale": "$locale",
  "registrationSiteId": "$registrationSiteId",
  "receiveEmail": "$receiveEmail",
  "dateOfBirth": "$dob",
  "mobileNumber": "$mobileNumber",
  "gender": "$gender",
  "fuelActivationDate": "$fuelActivationDate",
  "postalCode": "$postalCode",
  "country": "$country",
  "city": "$city",
  "state": "$state",
  "bio": "$bio",
  "jpFirstNameKana": "$jpFirstNameKana",
  "jpLastNameKana": "$jpLastNameKana",
  "height": "$height",
  "weight": "$weight",
  "distanceUnit": "MILES",
  "weightUnit": "POUNDS",
  "heightUnit": "FT/INCHES"
}
EOF
于 2019-09-05T06:43:57.330 回答
0

将数据放入 txt 文件对我有用

bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
curl --version
curl 7.29.0 (x86_64-redhat-linux-gnu)
 cat curl_data.txt 
 {  "type":"index-pattern", "excludeExportDetails": true  }

curl -X POST http://localhost:30560/api/saved_objects/_export -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d "$(cat curl_data.txt)" -o out.json
于 2021-01-14T16:30:47.163 回答