当我echo时,我得到这个,当我把它输入到终端时,它就会运行

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"

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


当前回答

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中调用subshell和引用变量。黑客希望这有助于与快乐 '"'"'""""'''""''.

其他回答

工作很好,刚刚创建了post_data函数,并在shell调用中调用它。

这个脚本将使用指定的JSON主体向指定的URL发送POST请求,并将服务器的响应输出到控制台:

#!/bin/bash

# Set the URL to send the request to
url='http://example.com/endpoint'

# Set the JSON body of the request
json_data='{"key1": "value1", "key2": "value2"}'

# Make the POST request with the JSON body
response=$(curl -X POST -H "Content-Type: application/json" -d "$json_data" "$url")

echo "Response from server: $response"

您不需要将包含自定义标头的引号传递给curl。另外,数据参数中间的变量应该加引号。

首先,编写一个生成脚本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": "$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'。

现有的答案指出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

来自Athos爵士的信息非常有效!!

下面是我如何在我的curl脚本为couchDB使用它。这真的很有帮助 经常出去。谢谢!

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