当前位置:网站首页>Detailed explanation and examples of common parameters of curl

Detailed explanation and examples of common parameters of curl

2022-06-21 12:59:00 51CTO

curl brief introduction

curl Is an open source command line tool , It is based on network protocol , Specify URL Network transmission , After getting the data, there is no specific processing ( Such as :html And so on ), Display directly in " standard output "(stdout) On .

curl There are many supported network protocols , Include :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 and TFTP.

curl There are also many parameters , Here are some common parameters , It is recommended to save .

send out GET request

When curl Without any parameters ,curl Default issue GET request , The content returned by the server will not be parsed and will be displayed directly on the command line . Example :

curl http://www.csdn.net

     
  • 1.

Because you need to jump to HTTPS, So back 301:

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center>301 Moved Permanently</center>
<hr><center>openresty</center>
</body>
</html>

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

send out POST request

Use -d When parameters are ,header Of Content-Type Is automatically assigned to application/x-www-form-urlencoded, And send POST request . Example :

 curl -d 'user= Wanmao society &pwd=onemore' http://csdn.net/login

     
  • 1.

Because you need to jump to HTTPS, Also return to 301:

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center>301 Moved Permanently</center>
<hr><center>openresty</center>
</body>
</html>

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

send out json request

send out json The request also requires two parameters :-X Parameter assignment HTTP Requested method ,-H Parameter assignment HTTP Requested header. Example :

curl -X POST -H "Content-Type: application/json; charset=UTF-8" -d '{"user":" Wan Maoxue ","pwd":"onemore"}' http://www.csdn.net/login

     
  • 1.

among ,-X Parameter assignment HTTP The request method is POST,-H Nibble at specified header Of Content-Type by application/json; charset=UTF-8 ,-d The parameter specifies that the data is {“user”:“ Wan Maoxue ”,“pwd”:“onemore”} .

Show HTTP Response head

-i The parameter displays the response content of the server at the same time , It also shows HTTP Response head . Example :

curl -i http://www.csdn.net

     
  • 1.

The response header of the server will be displayed first , Then leave a blank line , Then display the server response content , as follows :

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>

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

Show the response process

-v The whole response process of parameter display , We can see what happened at the bottom . Example :

curl -v http://www.csdn.net

     
  • 1.

It is shown as follows :

* 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>

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

among , With * The first line indicates curl Additional information provided , With > The first line represents the request header , < The first line represents the response header .

Only response headers are displayed

Sometimes the response content is too long , When you only care about the response header , have access to -I Parameters . Example :

curl -v http://www.csdn.net

     
  • 1.

It is shown as follows :

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/

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Reference link :
 https://curl.se/docs/manpage.html
 https://www.ruanyifeng.com/blog/2019/09/curl-reference.html

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211232344081.html