curl常用參數詳解及示例


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><h1>301 Moved Permanently</h1></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><h1>301 Moved Permanently</h1></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><h1>301 Moved Permanently</h1></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><h1>301 Moved Permanently</h1></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


竟然已經看到這里了,你我定是有緣人,留下你的點贊關注,他日必成大器。

微信公眾號:萬貓學社

微信掃描二維碼

關注后回復「電子書」

獲取12本Java必讀技術書籍


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM