在bash脚本函数中使用curl POST来定义variables

当我回应我得到这个,当我进入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 

但是当在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" 

我假设我的引号有问题,但是我和他们玩过很多,我也遇到了类似的错误。 所有的variables都是在实际的脚本中用不同的函数定义的

您不需要传递封闭自定义标题的引号来curl。 此外,您应该引用data参数中间的variables。

首先,编写一个函数来生成脚本的发布数据。 这样可以避免所有关于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' )中的单引号除了传递所有文本(包括双引号字符和美元符号)之外,都是一样的。

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

解决scheme使用https://httpbin.org/和内联bash脚本进行testing
1.对于没有空格的variables即1
只需在$variable之前和之后添加所需的string即可

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

2.用空格input:
用额外的" 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 

哇作品:)

Curl可以从一个文件发布二进制数据,所以我一直在使用进程replace,并利用文件描述符,每当我需要张贴一些令人讨厌的curl,并仍然希望访问当前shell中的variables。 就像是:

 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中调用subhells和whatnot以及参考variables。 快乐的黑客希望这有助于'"'"'""""'''""''

  • 从Athos先生的信息完美地工作!

以下是我在curl脚本中使用couchDB的方法。 这真的帮了很多。 谢谢!

 bin/curl -X PUT "db_domain_name_:5984/_config/vhosts/$1.couchdb" -d '"/'"$1"'/"' --user "admin:*****"