curl发送请求简单实践
来源:互联网 发布:全国医院数据库 编辑:程序博客网 时间:2024/05/18 02:03
环境
操作系统: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 $timecurl -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 PUTcontent type: -H "Content-Type: application/x-www-form-urlencoded" -H "Content-Type: application/json"数据参数: form urlencoded: -d "param1=value1¶m2=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¶m2=value2" -X POST http://localhost:3000/data
指明:
curl -d "param1=value1¶m2=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¶m2=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/jsonapp.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencodedapp.post('/data', function (req, res) { console.log(req.body); res.end();});app.listen(3000);
对比完后,我觉得我没有写错,但是POST
的json
形式就是不行。
唯一的区别就是,参数官网是写死的,而我使用的是变量。
最后我改成GET
请求:
#!/bin/bashset i=0while truedoappKey="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" ]]thenecho "循环次数"$ibreakelsesleep 2sfilet "i=i+1"echo "循环次数"$idone
因为我这个脚本是在重启项目后,再执行的,而脚本里又要调接口,所以需要放在循环里,不停的执行,直到成功为止。(因为项目刚启动时,接口未必能立马调通)。
参数-s
在执行脚本时,其会打印一些不想要的东东:
类似如下:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 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
- curl发送请求简单实践
- PHP CURL 发送请求
- curl命令发送请求
- CURL发送HTTP请求
- curl发送请求总结
- curl 发送http 请求
- HttpClient发送Get、Post请求简单实践
- curl 的安装及用curl发送简单的post请求
- curl命令发送Post请求
- PHP CURL发送POST请求
- curl 模拟http发送请求
- 用curl 发送 post 请求
- php CURL 发送请求详解
- bat curl 发送http请求 监控网站
- curl 发送HTTP请求 常用用法
- 使用curl和wget发送post请求
- PHP使用CURL发送HTTP请求代码
- curl发送post和get请求
- SSM,在后台打印SQL
- android IPC笔记
- SpringMVC通过切面,实现超灵活的注解式数据校验
- JS——密码强度
- AES加密异常处理 InvalidAlgorithmParameterException:expected IV length of 16 but was 32
- curl发送请求简单实践
- MySQL优化之表结构优化的5大建议(数据类型选择讲的很好)
- Boost 随机数库的使用
- jetson 使用技巧
- 阿里云Elasticsearch的X-Pack:机器学习、安全保障和可视化
- GreenDao报Can't replace method in ...\mvp\model\BrandModel.java:120 with generated version.错误的解决办法
- Java获取当前时间及String、datetime、date相互转化
- No Identifier specified for entity
- 标准的json格式