白日依山尽,黄河入海流。欲穷千里目,更上一层楼。 -- 唐·王之涣

linux curl请求中的单引号、双引号及变量

4.1、单引号、双引号结合使用

参数是在单引号中,比如 'Content-type:application/json'
json中的 k-v 是要在双引号中,所以如果遇到要在curl中使用变量,就使用 字符串拼接

1
2
3
curl -i -X POST -H 'Content-type:application/json' \
-d '{"msgtype": "text", "text": {"content": "'$warnmsg'"}}' \
'https://oapi.dingtalk.com/robot/send?access_token=xxx'

4.2、全部使用双引号

不方便的地方在于json中的k-v都需要使用双引号,那就需要进行转移",如果json中的k-v很多,那就书写有点麻烦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
curl -X POST -H "Content-Type:application/json"  \
-d "{\"userid_list\": \"${userid}\" ,\"msg\": {\"msgtype\": \"text\", \"text\": {\"content\": \"${build_msg}\"}}}" "https://oapi.dingtalk.com/robot/send?access_token=xxx"
​```

## 补充

1、curl下载文件

+ -o filename

`-o` 参数需要后面紧跟一个自定义的文件名

所以除了URL是具体的文件地址之外,URL也可以是非具体地址,比如 https://www.baidu.com 会报错整个网页的内容

+ -O

该参数后面跟的URL地址只能是具体的文件地址

另外

`-C` 可以实现断点续传
`-#` 显示下载进度
`-s` 静默输出
`-A` 模拟浏览器,比如`Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)`
`-e` 伪造referer(盗链)

2、监控网页请求性能

核心是利用`-w`参数(--write-out)使用curl内置的标识来输出请求过程的一些性能数据,比如状态码、总响应时长、DNS解析时长等

比如

```bash
curl -s -o /dev/null -w "%{http_code} %{remote_ip} %{time_total}" https://www.baidu.com

输出了状态码、用户IP和响应时长

具体的标识有:

content_type
filename_effective
ftp_entry_path
http_code
http_connect
local_ip
local_port
num_connects
num_redirects
redirect_url
remote_ip
remote_port
size_download
size_header
size_request
size_upload
speed_download
speed_upload
ssl_verify_result
time_appconnect
time_connect
time_namelookup
time_pretransfer
time_redirect
time_starttransfer
time_total
url_effective

linux curl请求中的单引号、双引号及变量

http://blog.colinspace.com/2022/02/07/linux-curl-variable/

作者

Colin

发布于

2022-02-07

许可协议