使用curl POST和在bash脚本函数中定义的变量 [英] Using curl POST with variables defined in bash script functions
问题描述
当我回音时,我得到这个,当我进入终端时运行。
curl -i \
-HAccept:application / json\
-HContent-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: ,city:Beverton,state:OR,bio:这是测试用户,jpFirstNameKana:不确定,jpLastNameKana:ofthis 80,weight:175,distanceUnit:MILES,weightUnit:POUNDS,heightUnit:FT / INCHES} https:// xxx:xxxxx @ xxxx-www .xxxxx.com / xxxxx / xxxx / xxxx
但是当在bash脚本文件中运行时,此错误
curl:(6)无法解析host:application; nodename或servname提供,或不知道
curl:(6)无法解析主机:is; nodename或servname提供,或不知道
curl:(6)无法解析主机:a; nodename或servname提供,或不知道
curl:(6)无法解析host:test; nodename或servname提供,或不知道
curl:(3)[globbing]不匹配关闭大括号/括号在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: :'$ locale',registrationSiteId:'$ registrationSiteId',receiveEmail:$ receiveEmail',dateOfBirth:$ dob',mobileNumber: ',gender:$ gender',fuelActivationDate:$ fuelActivationDate',postalCode:'$ postalCode',country :'$ city',state:'$ state',bio:$ bio',jpFirstNameKana:$ jpFirstNameKana',jpLastNameKana ,height:'$ height',weight:'$ weight',distanceUnit:MILES,weightUnit:POUNDS,heightUnit:FT / INCHES} '''https:// xxx:xxxxx@xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx b $ b 我假设我的引号有问题,但我玩过他们很多,我得到了类似的错误。所有变量在实际脚本中用不同的函数定义
解决方案您不需要传递包含自定义头文件的引号卷曲。另外,在 data
参数中间的变量应该引用。
首先,编写一个函数生成你的脚本的post数据。这可以避免你对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,
bio:$ bio,
jpFirstNameKana:$ jpFirstNameKana,
jpLastNameKana:$ jpLastNameKana,
height:$ height,
weight:$ weight,
distanceUnit:MILES,
weightUnit:POUNDS,
heightUnit:FT / INCHES
}
EOF
}
在调用curl时很容易使用该函数:
curl -i \
-HAccept:application / json\
-HContent-Type:application / json\
-X POST --data$(generate_post_data)https:// xxx :xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx
这里说有关shell引用规则的一些说明:
-H
参数中的双引号(如 -Hfoo bar
)告诉bash将内部的内容保存为单个参数(即使它包含空格)。
- data
参数中的单引号(如 - data'foo bar'
) ,除了它们逐字传递所有文本(包括双引号字符和美元符号)。
要在单引号文本的中间插入变量,必须结束单引号,然后与双引号变量连接,并重新打开单引号继续文本:'foo bar'$ variable'more foo'
。
When I echo I get this, which runs when I enter it into the terminal
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
But when run in the bash script file, I get this error
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
this is the code in the file
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"
I assume there's an issue with my quotation marks, but I've played with them a lot and I've gotten similar errors. All the variables are defined with different functions in the actual script
解决方案 You don't need to pass the quotes enclosing the custom headers to curl. Also, your variables in the middle of the data
argument should be quoted.
First, write a function that generates the post data of your script. This saves you from all sort of headaches concerning shell quoting and makes it easier to read an maintain the script than feeding the post data on curl's invocation line as in your attempt:
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
}
It is then easy to use that function in the invocation of 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"
This said, here are a few clarifications about shell quoting rules:
The double quotes in the -H
arguments (as in -H "foo bar"
) tell bash to keep what's inside as a single argument (even if it contains spaces).
The single quotes in the --data
argument (as in --data 'foo bar'
) do the same, except they pass all text verbatim (including double quote characters and the dollar sign).
To insert a variable in the middle of a single quoted text, you have to end the single quote, then concatenate with the double quoted variable, and re-open the single quote to continue the text: 'foo bar'"$variable"'more foo'
.
这篇关于使用curl POST和在bash脚本函数中定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!