CURL命令常用用法 您所在的位置:网站首页 curl读法 CURL命令常用用法

CURL命令常用用法

2023-12-17 01:10| 来源: 网络整理| 查看: 265

CURL命令常用用法 1 语法及常用选项1.1 语法1.2 选项 2 HTTP接口调用示例2.1 GET请求2.2 POST请求 3 WebService接口调用示例

本文参考文章如下,本文书写内容有限,想了解更多的可以看下面列举的参考文章: https://www.coonote.com/linux/linux-cmd-curl.html https://www.ruanyifeng.com/blog/2011/09/curl.html https://www.ruanyifeng.com/blog/2019/09/curl-reference.html

1 语法及常用选项 1.1 语法 curl (选项) (参数) 1.2 选项

这里只列举几个我认为比较常用的选项,想要了解更多,可以查看https://www.coonote.com/linux/linux-cmd-curl.html。

选项说明-H / --header自定义头信息-b / --cookie cookie字符串或文件读取位置-d / --dataHTTP POST方式传送数据-X / --request指定什么命令-i / --include输出时包含回应的标头信息-I / --head只输出回应的头信息-v输出通信的整个过程,用于调试

▉ -H  用于添加HTTP请求头信息

单个请求头信息添加:

curl -H "Content-Type: application/json" http://www.test.com

或者

curl --header "Content-Type: application/json" http://www.test.com

多个请求头信息添加:

curl -H "Content-Type: application/json" —H "Accept-Language: zh-CN" http://www.test.com

等同于

curl -H "Content-Type: application/json; Accept-Language: en-US" http://www.test.com

▉ -b  用于向服务器发送Cookie数据

curl -b "name=value" http://www.test.com

或者从cookiesFile中获取到cookie信息

curl -b cookiesFile http://www.test.com

▉ -d  用于发送 POST 请求中请求体Body数据

例如:

curl -H 'Content-Type: application/json' -X POST -d '{"name": "july","password": "123456"}' http://www.test.com

将请求体数据放入文件中,配置-d @filename,那么就可以从filename文件中获取到请求体数据,如下:

curl -H 'Content-Type: application/json' -X POST -d @data.txt http://www.test.com

另,需注意,如果请求体数据采用json格式,一定要配置请求头Content-Type为application/json,因为使用-d参数后,HTTP 请求会自动添加请求头Content-Type为application/x-www-form-urlencoded,将请求转为 POST 方法(因此可以省略-X POST)。

▉ -X  用于指定 HTTP 的请求方式(GET、POST等)

发送POST请求:

curl -X POST http://www.test.com

发送GET请求:

curl -X GET http://www.test.com

发送DELETE请求:

curl -X DELETE http://www.test.com

▉ -i  输出时包含回应的标头信息

命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码或返回信息。

D:\>curl -i https://www.baidu.com HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Connection: keep-alive Content-Length: 2443 Content-Type: text/html Date: Mon, 30 May 2022 12:33:22 GMT Etag: "588603e2-98b" Last-Modified: Mon, 23 Jan 2017 13:23:46 GMT Pragma: no-cache Server: bfe/1.0.8.18 Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ 百度一下,你就知道 新闻 hao123 地图 视频 贴吧 登录 document.write('登录'); 更多产品 关于百度 About Baidu ©2017;Baidu;使用百度前必读; 意见反馈;京ICP证030173号; D:\>

▉ -I  只输出回应的头信息

命令收到服务器回应后,只输出服务器回应的标头,不再输出网页的源码或返回信息。

D:\>curl -I https://www.baidu.com HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Connection: keep-alive Content-Length: 277 Content-Type: text/html Date: Mon, 30 May 2022 12:33:32 GMT Etag: "575e1f59-115" Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT Pragma: no-cache Server: bfe/1.0.8.18 D:\>

▉ -v  输出通信的整个过程,用于调试

如以下为GET请求调用http://localhost:8082/word?keyWord=water的打印内容:

D:\>curl -v http://localhost:8082/word?keyWord=water * Trying 127.0.0.1:8082... * Connected to localhost (127.0.0.1) port 8082 (#0) > GET /word?keyWord=water HTTP/1.1 > Host: localhost:8082 > User-Agent: curl/7.79.1 > Accept: */* > * Mark bundle as not supporting multiuse


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有