curl常用参数详解及示例
source link: https://blog.51cto.com/u_6740480/5398382
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
curl简介
curl是一个开源的命令行工具,它基于网络协议,对指定URL进行网络传输,得到数据后不任何具体处理(如:html的渲染等),直接显示在"标准输出"(stdout)上。
curl支持的网络协议有很多,包括:DICT、FILE、FTP、FTPS、GOPHER、GOPHERS、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、MQTT、POP3、POP3S、RTMP、RTMPS、RTSP、SCP、SFTP、SMB、SMBS、SMTP、SMTPS、TELNET和TFTP。
curl的参数也有很多,下面介绍一些常用的参数,建议收藏保存。
发送GET请求
当curl不带有任何参数时,curl默认发出 GET 请求,服务端返回的内容不会做任何解析直接在命令行显示。示例:
curl http://www.csdn.net
因为需要跳转到HTTPS,所以返回301:
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center>301 Moved Permanently</center>
<hr><center>openresty</center>
</body>
</html>
发送POST请求
使用-d
参数时,header的Content-Type
被自动赋值为application/x-www-form-urlencoded
,并且发送 POST 请求。示例:
curl -d 'user=万猫学社&pwd=onemore' http://csdn.net/login
因为需要跳转到HTTPS,同样返回301:
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center>301 Moved Permanently</center>
<hr><center>openresty</center>
</body>
</html>
发送json请求
发送json请求还需要用到两个参数:-X
参数指定 HTTP 请求的方法,-H
参数指定 HTTP 请求的header。示例:
curl -X POST -H "Content-Type: application/json; charset=UTF-8" -d '{"user":"万猫学","pwd":"onemore"}' http://www.csdn.net/login
其中,-X
参数指定 HTTP 请求的方法为 POST,-H
蚕食指定header的 Content-Type 为 application/json; charset=UTF-8 ,-d
参数指定数据为 {“user”:“万猫学”,“pwd”:“onemore”} 。
显示HTTP响应头
-i
参数显示服务端响应内容的同时,也显示HTTP响应头。示例:
curl -i http://www.csdn.net
会先显示服务端的响应头,然后空一行,再显示服务端响应内容,如下:
HTTP/1.1 301 Moved Permanently
Server: openresty
Date: Thu, 20 Jan 2022 11:59:42 GMT
Content-Type: text/html
Content-Length: 166
Connection: keep-alive
Keep-Alive: timeout=20
Location: https://www.csdn.net/
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center>301 Moved Permanently</center>
<hr><center>openresty</center>
</body>
</html>
显示响应过程
-v
参数显示的整个响应过程,我们可以看到底层到底发生了什么。示例:
curl -v http://www.csdn.net
显示如下:
* About to connect() to www.csdn.net port 80 (#0)
* Trying 39.106.226.142...
* Connected to www.csdn.net (39.106.226.142) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.csdn.net
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: openresty
< Date: Thu, 20 Jan 2022 12:07:40 GMT
< Content-Type: text/html
< Content-Length: 166
< Connection: keep-alive
< Keep-Alive: timeout=20
< Location: https://www.csdn.net/
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center>301 Moved Permanently</center>
<hr><center>openresty</center>
</body>
</html>
其中,以*
开头的行表示curl提供的额外信息,以>
开头的行表示请求头, <
开头的行表示响应头。
只显示响应头
有时候响应内容太长,只关心响应头时,可以使用-I
参数。示例:
curl -v http://www.csdn.net
显示如下:
HTTP/1.1 301 Moved Permanently
Server: openresty
Date: Thu, 20 Jan 2022 12:15:30 GMT
Content-Type: text/html
Content-Length: 166
Connection: keep-alive
Keep-Alive: timeout=20
Location: https://www.csdn.net/
参考链接:
https://curl.se/docs/manpage.html
https://www.ruanyifeng.com/blog/2019/09/curl-reference.html
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK