发起 Http 请求,一个 cURL 足矣

2023-05-06 04:00:51

 

cURL是什么?c 可以看作是 client,url(Uniform Resource Locator)是统一资源定位符。cURL 可以为指定的 url 执行网络传输,在 shell 和脚本中它是非常便捷、强大、可靠的。

cURL 支持 N 多协议(ftp、smtp等等),本文只讨论有关 http 基于命令行的相关话题,使用 cURL 完全可以轻而易举地取代 postman 之流的图形界面工具。下面看下使用 cURL 发起 http 请求。

使用 cURL 发起 http 请求发起 http get 请求curl http://localhost:8080/demo使用-v 详细显示请求响应相关信息curl -v http://localhost:8080/demo使用-G -d 发起get请求并发送数据curl -G -d "hello" -v http://1ocalhost:8080/demo使用-I 发起head请求curl -I http://localhost:8080/demo使用-i 响应包含头部信息curl -i http://localhost:8080/demo

以上是基本的get请求示例,下面看下使用curl发起需要登录认证的请求。

使用 cURL 发起需要登录认证的请求使用-u 提供用户名密码curl -u admin:admin http://localhost:9002/actuatorcurl自动识别用户名密码curl http://admin:admin@localhost:9002/actuator 1使用-u 仅输入用户名 会提示密码输入curl -u admin http://localhost:9002/actuator使用-c 保存服务端响应的cookiecurl -u admin:admin -c cookie.txt http://localhost:9002/actuator使用-b 携带cookie信息发起http请求curl -b cookie.txt http://localhost:9002/actuator

下面看下使用curl发送post请求。

使用 cURL 发送 post 请求使用-d 发送http post请求数据 -H指定head line头信息curl -d "{name:star,age:20}" -H "Content-type:application/json" http://localhost:8080/demo/post使用@引用文件 包含请求数据的文件curl -d @post_data -H "Content-type:application/json" http://localhost:8080/demo/post使用-F选项 post上传文件curl -F fileName=@curl.pnghttp://localhost:8080/demo/file使用–data-urlencode编码 提交数据curl --data-urlencode name=码农小麦 -v http://localhost:8080/demo/urlencode使用-d 提交请求数据curl -d name=码农小麦 -d content=欢迎来撩 -v http://localhost:8080/demo/post curl -d name=码农小麦&content=欢迎来撩 -v http://localhost:8080/demo/post 1

以上就是 cURL 常见的命令行使用示例,完全可以应对日常的开发测试场景,以及脚本相关 http 请求功能实现。更多使用方法参见 curl --help。


以上就是关于《发起 Http 请求,一个 cURL 足矣》的全部内容,本文网址:https://www.7ca.cn/baike/22819.shtml,如对您有帮助可以分享给好友,谢谢。
标签:
声明

排行榜