4

curl常用参数详解及示例

 2 years ago
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.
neoserver,ios ssh client

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

  • 23
    • www.wenyuanblog.com 4 years ago
    • Cache

    curl 常用命令参考

    curl 是常用的命令行工具,用来请求 Web 服务器。平时运维过程中使用频率较高。 curl 是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在「标准输出」(stdout)上面。 它支持多种协议,下面整理平时运...

  • 8

    所有接口的响应参数和响应示例为空,其他功能都正常 - OSCHINA - 中文开源技术交流社区 开源问答...

  • 16

    前言¶ 前面 pref event 示例中我们是通过 bpf_probe_read(&data.file_name, sizeof(data.file_name), PT_REGS_PARM2(ctx))...

  • 7

    LINUX下循环读取文件参数并CURL远程API 一系列参数存于文本文件,需在LINUX下循环读取,之后以此参数进行CURL远程API调用,同时需记录每次CURL的总时间

  • 11
    • caihongtengxu.github.io 3 years ago
    • Cache

    PHP CURL详解

    CURL是一个功能强大的开源类库. PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器、使用各种协议。libcurl 目前支持的协议有 http、https、ftp、gopher、telnet、dict、file、ldap。 libcurl 同时支持 HTTPS 证...

  • 5
    • walkerdi.github.io 3 years ago
    • Cache

    curl 参数加解密方案

    curl 参数加解密方案 在web 应用开发中,我们通常会对一些敏感参数进行加密,来提高安全性,这里提供了一种在curl 中加密和解密的实现方案,可以自定义密匙和有效期。具体实现代码如下: /** * Des...

  • 4
    • xugaoxiang.com 2 years ago
    • Cache

    Linux常用命令-84:curl

    迷途小书童的Note curl 是一个文件传输工具,可以上传也可以下载,支持 HTTP、

  • 5
    • www.myfreax.com 2 years ago
    • Cache

    Curl命令示例 | myfreax

    linux Linux Curl命令示例 Curl是一种命令行实用程序,用于从服务器传输数据或向服务器传输数据,该服务器设计为无需用户交互即可工作。使用curl时,您可以使用...

  • 8

    Terraform 中配置代码的参数详解及常用命令参数介绍 兰玉磊 • 2小时前 • 其他 • 阅读 2

  • 6

    影响Nginx访问和下载的常用配置参数详解 作者:微技术 2024-01-15 16:46:35 服务器 在使用Nginx做代理服务器的过程中,我们有时会遇到需要临...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK