curl发送请求简单实践

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013066244/article/details/78782459

环境

操作系统:win7
服务器:centos6.5

场景

因为有个脚本需要模拟浏览器发送api请求,该api是用来初始化数据的。
具体脚本如下:

appKey="TKtXLPUuGkfaRHA"
appSecret="aArwoGW9dYXBGL79oR8Ns3ZXCkPcGFdb"
suffurl="v1/gghq/launch"
time=`date +%s`
sign=`java -Xbootclasspath/a:/home/webuser/gogoal_platform/ggopenapi/lib/snakeyaml.jar -jar ./ggsign/ggsign.jar  "$appKey" "$appSecret" "$time" "$suffurl"`
#echo "$sign"

curl -X POST -d "app_key=TKtXLPUuGkfaRHA&time_stamp=1512612452&sign=U7ETnXEaPUu1jNMq4jKf00fiGSM%3D" http://ggservice.sandbox.gofund.com.cn/v1/gghq/launch

这里我是使用的是post请求,参数是使用&来进行拼接,这种方式,是可以发送的,并成功的!

但是我如果改成json的形式来发送时总是会报错,就是服务器端接收不到参数:

appKey="TKtXLPUuGkfaRHA"
appSecret="aArwoGW9dYXBGL79oR8Ns3ZXCkPcGFdb"
suffurl="v1/gghq/launch"
time=`date +%s`
sign=`java -Xbootclasspath/a:/home/webuser/gogoal_platform/ggopenapi/lib/snakeyaml.jar -jar ./ggsign/ggsign.jar  "$appKey" "$appSecret" "$time" "$suffurl"`
echo "$sign"
echo $time

curl -H "Content-type: application/json" -X POST -d '{"app_key":"'$appKey'","time_stamp":"'$time'","sign":"'$sign'"}' http://ggservice.sandbox.gofund.com.cn/v1/gghq/launch

查了下官网curl教程:

post

参数:

必须类型参数:

    -X POST
    -X PUT
content type-H "Content-Type: application/x-www-form-urlencoded"

    -H "Content-Type: application/json"

数据参数:
    form urlencoded: -d "param1=value1&param2=value2" or -d @data.txt
    json: -d '{"key1":"value1", "key2":"value2"}' or -d @data.json

官网例子:

POST application/x-www-form-urlencoded

application/x-www-form-urlencoded是默认的:

curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data

指明:

curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data

使用数据文件的话:

curl -d "@data.txt" -X POST http://localhost:3000/data

POST application/json

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data

使用数据文件的话:

curl -d "@data.json" -X POST http://localhost:3000/data

data.json

{
  "key1":"value1",
  "key2":"value2"
}

data.txt

param1=value1&param2=value2

package.json

{
  "name": "postdemo",
  "version": "1.0.0",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "^1.15.0",
    "express": "^4.13.4"
  }
}

server.js

var app = require('express')();
var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

app.post('/data', function (req, res) {
  console.log(req.body);
  res.end();
});

app.listen(3000);

对比完后,我觉得我没有写错,但是POSTjson形式就是不行。

唯一的区别就是,参数官网是写死的,而我使用的是变量。

最后我改成GET请求

#!/bin/bash
set i=0
while true
do
appKey="TKtXLPUuGkfaRHA"
appSecret="aArwoGW9dYXBGL79oR8Ns3ZXCkPcGFdb"
suffurl="v1/gghq/launch"
time=`date +%s`
sign=`java -Xbootclasspath/a:/home/webuser/gogoal_platform/ggopenapi/lib/snakeyaml.jar -jar ./ggsign/ggsign.jar  "$appKey" "$appSecret" "$time" "$suffurl"`
#echo "$sign"
#echo $time
#url="http://ggservice.sandbox.gofund.com.cn/v1/gghq/launch"
url="http://ggservice.sandbox.gofund.com.cn/v1/gghq/launch?app_key=$appKey&time_stamp=$time&sign=$sign"
echo "$url"
#curl -H "Content-type: application/json" -X POST -d '{"app_key":"TKtXLPUuGkfaRHA","time_stamp":"1512612452","sign":"U7ETnXEaPUu1jNMq4jKf00fiGSM%3D"}' http://ggservice.sandbox.gofund.com.cn/v1/gghq/launch
#curl -X POST -d "app_key=TKtXLPUuGkfaRHA&time_stamp=1512612452&sign=U7ETnXEaPUu1jNMq4jKf00fiGSM%3D" http://ggservice.sandbox.gofund.com.cn/v1/gghq/launch
#$echo $url
#curlstr=`curl -i -X GET -H "'Content-type':'application/json'" $url -s`
curlstr=`curl -X GET -H "'Content-type':'application/json'" $url -s`
echo "$curlstr"

if [[ "$curlstr" =~ "成功" || "$curlstr" =~ "success" ]]
then
echo "循环次数"$i
break
else
sleep 2s
fi
let "i=i+1"
echo "循环次数"$i
done

因为我这个脚本是在重启项目后,再执行的,而脚本里又要调接口,所以需要放在循环里,不停的执行,直到成功为止。(因为项目刚启动时,接口未必能立马调通)。

参数-s

在执行脚本时,其会打印一些不想要的东东:

类似如下:

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  25963      0 --:--:-- --:--:-- --:--:-- 25880

可以添加参数-s去掉这些统计信息。

变量的使用

虽然上面我使用变量没有成功,但是在stackoverflow,对变量的使用有个说明:

使用'"的组合:

now=$(date +"%m-%d-%Y")
curl -X POST -H "Content-Type: application/json" \
    --data '{ "color":"red", "message":"Build failed '"$now"'", "message_format":"text" }' \
    https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>

对我的例子就是

appKey="TKtXLPUuGkfaRHA"
appSecret="aArwoGW9dYXBGL79oR8Ns3ZXCkPcGFdb"
suffurl="v1/gghq/launch"
time=`date +%s`
sign=`java -Xbootclasspath/a:/home/webuser/gogoal_platform/ggopenapi/lib/snakeyaml.jar -jar ./ggsign/ggsign.jar  "$appKey" "$appSecret" "$time" "$suffurl"`
echo "$sign"
echo $time
# 注意变量的引号
curl -H "Content-type: application/json" -X POST -d '{"app_key":"'"$appKey"'","time_stamp":"'"$time"'","sign":"'"$sign"'"}' http://ggservice.sandbox.gofund.com.cn/v1/gghq/launch

只使用双引号:

now=$(date +"%m-%d-%Y")
curl -X POST -H "Content-Type: application/json" \
    --data "{ \"color\":\"red\", \"message\":\"Build failed $now\", \"message_format\":\"text\" }" \
    https://api.hipchat.com/v2/room/<room>/notification?auth_token=<token>

对我的例子就是

# 以上省略。。。
curl -H "Content-type: application/json" -X POST -d "{\"app_key\":\"$appKey\",\"time_stamp\":\"$time\",\"sign\":\"$sign\"}" http://ggservice.sandbox.gofund.com.cn/v1/gghq/launch

说明:

使用'包裹变量,将使bash,从字面值上去看待它们;
然而使用",将会用它们的变量值来进行替换。

参考地址:

Curl不显示统计信息% Total % Received %

subfuzion/curl.md

https://stackoverflow.com/a/26614517/6952713

展开阅读全文

没有更多推荐了,返回首页