当我回音时,我得到了这个,当我把它输入终端时运行
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"
我认为我的引号存在问题,但我和他们玩了很多,我也遇到了类似的错误。所有变量都在实际脚本中使用不同的函数定义
答案 0 :(得分:207)
您无需将包含自定义标头的引号传递给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'
。
答案 1 :(得分:57)
使用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。对于带空格的输入:
使用附加"
包裹变量,即"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
哇工作:)
答案 2 :(得分:25)
Curl可以从文件中发布二进制数据,所以我一直在使用进程替换并利用文件描述符,每当我需要发布一些讨厌的curl并且仍然想要访问当前shell中的vars时。类似的东西:
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中调用子壳以及诸如此类的参考变量。快乐的黑客希望这有助于'"'"'""""'''""''
。
答案 3 :(得分:6)
迟了几年但如果你使用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/"$//')
答案 4 :(得分:4)
以下是我在couchDB的curl脚本中使用它的方法。它确实有帮助 出了很多。谢谢!
bin/curl -X PUT "db_domain_name_:5984/_config/vhosts/$1.couchdb" -d '"/'"$1"'/"' --user "admin:*****"
答案 5 :(得分:4)
我们可以使用单引号 '
为 curl 分配一个变量,并将一些其他变量括在双单双引号 "'"
中,以便curl-variable 内的替换。然后我们可以轻松地使用 curl-variable,这里是 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"
答案 6 :(得分:2)
在这里的答案指导下,这实际上是对我有用的东西:
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'
答案 7 :(得分:0)
现有的答案指出,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
答案 8 :(得分: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