当我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"

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


当前回答

这个脚本将使用指定的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可以从文件中发布二进制数据,所以我一直在使用进程替换和利用文件描述符,每当我需要发布一些讨厌的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和引用变量。黑客希望这有助于与快乐 '"'"'""""'''""''.

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

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

2. 对于带空格的输入: 用额外的“即”来包装变量。“el”:

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

哇工作:)

乔可能会帮忙:

$ export A=this B=that C=foo D=bar
$ jo -p a=$A b=$B nested=$(jo c=$C d=$D)
{
   "a": "this",
   "b": "that",
   "nested": {
      "c": "foo",
      "d": "bar"
   }
}

您不需要将包含自定义标头的引号传递给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'。

把数据放入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