linux环境下curl工具常用参数和用法

对于开发http请求处理服务器端程序来以及配置nginx反向代理来说,经常需要有http请求来验证nginx配置的正确性,以及后端接口实现的正确性,在windows环境下,经常使用图形界面的浏览器或者postman工具来发送http请求,但是有时只有linux环境的时候,这两种方式都无能为力了,这个时候,就可以使用curl工具来满足对应的需求。

curl 是一个强大的命令行工具,用于与服务器进行数据传输。它支持多种协议(如 HTTP、HTTPS、FTP 等),并且提供了丰富的参数来配置请求。以下是一些常用的 curl 参数及其含义和用法:

一、基本用法

1. -I / --head

获取响应头而不下载正文。

curl -I https://example.com
2. -X / --request

指定 HTTP 请求方法,例如 GET、POST、PUT、DELETE 等。

curl -X POST https://example.com
3. -d / --data

发送 POST 请求的数据。可以以键值对或 JSON 格式发送。

curl -d "key1=value1&key2=value2" -X POST https://example.com
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST https://example.com
4. -H / --header

添加 HTTP 请求头。

curl -H "Content-Type: application/json" https://example.com
5. -o / --output

将下载的内容保存到文件。

curl -o output.txt https://example.com
6. -O / --remote-name

使用远程文件的名称保存内容。

curl -O https://example.com/file.txt

二、进阶用法

7. -u / --user

使用用户名和密码进行身份验证。

curl -u username:password https://example.com
8. -F / --form

发送表单数据(多部分表单)。

curl -F "file=@/path/to/file" -F "key=value" https://example.com
9. -L / --location

自动跟随重定向。

curl -L https://example.com
10. -k / --insecure

允许不安全的 SSL 连接并跳过 SSL 证书验证。

curl -k https://example.com
11. -v / --verbose

详细模式,显示请求和响应的详细信息。

curl -v https://example.com
12. -s / --silent

静默模式,不输出进度和错误信息。

curl -s https://example.com
13. -w / --write-out

输出格式化信息,例如请求时间、HTTP 状态码等。

curl -w "%{http_code}\n" https://example.com
14. -C / --continue-at

断点续传,继续从上次中断的地方下载文件。

curl -C - -O https://example.com/largefile.zip
15. -e / --referer

设置请求的 Referer 头。

curl -e "https://referer.com" https://example.com
16. -A / --user-agent

设置请求的 User-Agent 头。

curl -A "Mozilla/5.0" https://example.com
17. -b / --cookie

发送 cookie 数据。

curl -b "name=value" https://example.com

或者从文件中读取 cookie:

curl -b cookies.txt https://example.com
18. -c / --cookie-jar

将服务器发送的 cookie 保存到文件。

curl -c cookies.txt https://example.com

三、示例

1. 下载文件并保存为 example.html
curl -o example.html https://example.com
2. 发送 JSON 数据的 POST 请求:
curl -d '{"name":"John", "age":30}' -H "Content-Type: application/json" -X POST https://example.com/api
3. 使用用户名和密码进行 HTTP 基本身份验证:
curl -u myusername:mypassword https://example.com/protected
4. 自动跟随重定向并显示详细信息:
curl -L -v https://example.com

5. 包含输出信息的案例

# curl -v  -H "authorization:NTLM TlRMTVNTUA" http://127.0.0.1/ngtest    
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /ngtest HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.61.1
> Accept: */*
> authorization:NTLM TlRMTVNTUA
> 
< HTTP/1.1 200 
< Server: nginx/1.14.1
< Date: Tue, 22 Oct 2024 15:48:33 GMT
< Content-Length: 0
< Connection: keep-alive
< Set-Cookie: mycookie=5ea7df386cd64620bfdef149cedb1a8f; Max-Age=3600; Expires=Tue, 22 Oct 2024 16:48:33 GMT; Path=/
< 
* Connection #0 to host 127.0.0.1 left intact

通过理解和使用这些 curl 参数,你可以更灵活和高效地与服务器进行数据传输和交互。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿20

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值