linux文件下载、服务器交互命令(curl)

简介:

CURL(command line URL) 是一个命令行工具,支持从服务器下载资源、发送HTTP请求、上传文件和处理数据等。它支持多种协议,包括 HTTP、FTP、SMTP 等。

使用方法:

curl [options...] <url>
  • / : 不带参数 默认为http返回网站元素,可以用来检测网站服务是否正常。例如:curl www.baidu.com
  • -o/--output <file> : 将输出写入指定的文件中。例如:curl -o output.txt https://example.com
  • -O/--remote-name : 使用 URL 最后一部分命名文件并将文件保存到当前目录中。例如:curl -O https://example.com/index.html
  • -L/--location : 跟随重定向。例如:curl -L http://shorturl.at/EI248
  • -v/--verbose : 显示详细的调试信息。例如:curl -v https://example.com
  • -u/--user <user:password> : 提供用户名和密码进行身份验证。例如:curl -u username:password https://example.com
  • -X/--request <command> : 指定 HTTP 请求方法。例如:curl -X POST https://example.com/data
  • -H/--header <header> : 为请求添加一个特定的标头。例如:curl -H "Content-Type: application/json" https://example.com
  • -d/--data <data> : 发送 POST 请求时使用一个数据段。例如:curl -d "key=value&key2=value2" https://example.com/post
  • -I/--head : 仅显示响应头信息。例如:curl -I https://example.com

更多参数可以通过 man curlcurl --help 命令或官方文档查看。

curl常用参数

-H设置header
-u设置用户名密码
-X设置请求的方式
-O源文件名保存文件
-o 指定文件名保存文件
-d post请求体

其他参数

curl --help

常用方式

保留原文件名称下载服务器文件

curl -O https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/lib/esl-d776bfb1aa.js

自定义文件名称下载服务器文件

curl -o test.js https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/lib/esl-d776bfb1aa.js

访问服务器返回数据

curl www.baidu.com

默认为http返回网站首页,可以用来检测网站服务是否正常。

访问服务器的某个网页

curl https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/lib/esl-d776bfb1aa.js

请求服务器接口

-X 设置请求方式为POST
-H 在header中设置请求类型Content-Type: application/json
-d post请求时提交的数据

curl -X POST "localhost:9200/knowledges/_doc?pretty" -H 'Content-Type: application/json' -d' {  "title": "测试",  "content":"优麒麟 ",  "create_time":1621589391000,  "update_time":1621589391000 }'

转载:https://www.bilibili.com/read/cv14845601

posted @ 2023-05-26 15:09  Journey&Flower  阅读(603)  评论(0编辑  收藏  举报