文章目录
1. curl介绍
- 1. curl介绍
- 2. 直接发送请求 (Get)
- 3. 发送Post请求
- 3.1 Post请求带参数
- 4. Put请求和Delete请求
- 5. 携带首部信息
- 6. 获取响应的所有的首部信息
- 7. 文件下载
- 8. 显示底层连接信息
- 9. 通过代理访问
- 10. 通过不同协议访问
curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
2. 直接发送请求 (Get)默认是Get请求
curl URL
相当于发送一个GET请求,curl默认就是发送GET请求
测试:
curl https://jsonplaceholder.typicode.com/posts/1
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
curl -X -POST URL
或者
curl -XPOST URL
测试
curl -XPOST https://jsonplaceholder.typicode.com/posts
{
"id": 101
}
curl -XPOST URL -d '{"key1":"value1","key2":"value2"}'
测试
curl -XPOST https://jsonplaceholder.typicode.com/posts -d '{"name":"张三", "age":"20"}'
{
"{\"name\":\"张三\", \"age\":\"20\"}": "",
"id": 101
}
4. Put请求和Delete请求
curl -XPUT URL 和 curl -XDELETE URL
带参数的
curl -XPUT URL -d '{}' 和 curl -XDELETE URL -d '{}'
测试put
curl -XPUT https://jsonplaceholder.typicode.com/posts/1 -d '{"name":"李四", "age":"10"}'
输出
{
"{\"name\":\"李四\", \"age\":\"10\"}": "",
"id": 1
}
5. 携带首部信息
curl -XPOST URL -H '' -H '' //可以携带多个首部信息
测试
curl -XPOST https://jsonplaceholder.typicode.com/posts -H 'Content-type:application/json'
-H 'A-cc-dept:ap-aption/json' -d '{"title":"张三"}'
{
"title": "张三",
"id": 101
}
6. 获取响应的所有的首部信息
curl -I URL // 使用大写I获取
测试
curl -I https://jsonplaceholder.typicode.com/posts
HTTP/2 200
date: Sun, 14 Aug 2022 10:02:42 GMT
content-type: application/json; charset=utf-8
x-powered-by: Express
x-ratelimit-limit: 1000
x-ratelimit-remaining: 999
x-ratelimit-reset: 1652476599
vary: Origin, Accept-Encoding
access-control-allow-credentials: true
cache-control: max-age=43200
pragma: no-cache
expires: -1
x-content-type-options: nosniff
etag: W/"6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM"
via: 1.1 vegur
cf-cache-status: HIT
age: 12762
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=t%2BwhJvMXQq7z8IdGOyEt7%2FgeW%2BRZy4MnLJ32vPvCHZA916xs20rYcy9zfbaAI9tGQ6372ztL5%2F1lXkg1bObDaorK9Zh7DoArRzxNfx%2FkJky3APOvo7Y1ryTscGC6dicHh2UO3JVdtbimr5Hyw8U%2F"}],"group":"cf-nel","max_age":604800}
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
server: cloudflare
cf-ray: 73a8db3e5b0d7b59-LAX
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
7. 文件下载
curl -O URL 直接下载到当前文件夹里面 下载之前记得cd切换文件夹
curl -o https://profile.csdnimg.cn/F/0/6/1_m0_37989980
输出:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 107 100 107 0 0 512 0 --:--:-- --:--:-- --:--:-- 532
8. 显示底层连接信息
curl -v URL //会显示握手信息等
9. 通过代理访问
curl --proxy 协议://用户名:密码@代理地址:端口 URL
curl --proxy "http://egg:123@127.0.0.1:1234" URL
10. 通过不同协议访问
curl -u 用户名:密码 -O ftp://server/aaa.avi 下载
curl -u 用户名:密码 -T 文件 ftp://server