Linux 使用 curl 命令发送带参请求

本文介绍了如何使用curl工具发送POST和GET请求,并演示了如何解析响应结果。包括请求头设置、参数传递及结果输出到文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 发送 post 请求(请求参数为 json 格式): 

curl -i -X POST -H 'Content-type':'application/json' -d '{"id":"'7'","token":"'LzvKzJ'"}'  url地址
# 不带请求头
curl -v -XPOST url地址 -d 'name=张三&age=14'

2.  发送 get 请求:

curl https://localhost:8080/user/getInfo?id=7\&userName=Janet

注:请求有多个参数时,& 之前要加转义符

3. 获取请求结果

把结果输出到一个文件中,再读取文件即可。

$ curl http://localhost:60001/api/measures/search?projectKeys=org.example:JanetTestProject\&metricKeys=bugs > result.log

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   105  100   105    0     0   2337      0 --:--:-- --:--:-- --:--:--  2386

$ cat result.log
{"measures":[{"metric":"bugs","value":"4","component":"org.example:JanetTestProject","bestValue":false}]}[

cut命令:

对result.log按照“,” 进行分割,并输出分割后第2列信息

$ cat result.log
{"measures":[{"metric":"bugs","value":"4","component":"org.example:JanetTestProject","bestValue":false}]}
 
$ cut -d "," -f 2 result.log
"value":"4"
 
$ cut -d '"' -f 10 result.log
4

awk 分割:

$ echo $(curl http://10.142.143.230:60001/api/measures/search?projectKeys=org.example:JanetTestProject\&metricKeys=bugs) | awk -F '"' '{print $10}'
4

$ echo "111||222|333"|awk 'BEGIN{FS="[|]+"}{print $1}'
111

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值