CURL命令 : GET、POST请求、文件下载等常用命令


1. curl介绍

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"
}

在这里插入图片描述

3. 发送Post请求

curl -X -POST URL 
或者 
curl -XPOST URL

测试

curl -XPOST  https://jsonplaceholder.typicode.com/posts

{
	"id": 101
}

在这里插入图片描述

3.1 Post请求带参数

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
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: curl命令默认的HTTP动词是GET,用于下载文件。如果要指定其他动词,可以使用-X参数,例如使用POST动词下载文件可以使用命令curl -X POST \[url\]。同样,如果要删除文件,可以使用DELETE动词,命令为:curl -X DELETE \[url\]。\[1\]另外,curl还提供了内置的option,如使用-O(大写)可以保存网页中的文件,命令为:curl -O \[url\]。需要注意的是,url需要具体到某个文件,否则无法下载。\[2\]此外,curl还支持上传文件的功能,可以使用内置option -T来实现,命令为:curl -T \[file\] -u \[username:password\] \[url\]。其中,\[file\]是要上传的文件名,\[username:password\]是FTP服务器的用户名和密码,\[url\]是上传的目标地址。\[3\]所以,如果要使用curl下载文件,可以使用命令curl -O \[url\]。 #### 引用[.reference_title] - *1* [curl命令详解 (curl -I)](https://blog.csdn.net/mao_xiaoxi/article/details/97764814)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Linux curl 命令下载文件](https://blog.csdn.net/julius_lee/article/details/124245136)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

white camel

感谢支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值