测试工具
可以用nc快速开启一个端口监听, 用来检查curl请求
GET 请求
| curl [URL] |
| |
| cURL -X GET [URL] |
例如
| curl http://127.0.0.1:8768/app/bootup/ping |
| curl -X GET http://127.0.0.1:8768/app/bootup/ping |
GET请求的参数一般放在链接里, 例如
| curl -X GET http://127.0.0.1:8768/app/data_import/get?id=1 |
如果要像POST那样放到 -d 参数中, 则需要加上 -G 参数
-G, --get Put the post data in the URL and use GET
例如
| curl -X GET -G -d 'id=1' http://127.0.0.1:8768/app/data_import/get |
POST 请求
用 -d 指定参数, 格式默认使用的是 Content-Type: application/x-www-form-urlencoded;charset=UTF-8
| cURL -X POST -d "k=v&k2=v2" |
如果需要使用 json, 需要用 -H 指定 Content-Type: application/json
| curl -X POST -H "Content-Type: application/json" -d '{}' [URL] |
例如
| curl -X POST -H "Content-Type: application/json" -d '{"page":2, "limit":2}' http://127.0.0.1:8768/app/static_file/list |
上传文件
Multipart 文件上传
| |
| curl -F key1=value1 -F upload=@localfilename URL |
例如
| curl -v -F file=@"/home/milton/Downloads/File_28_7.zip" http://127.0.0.1/app/static_file/upload |
| curl -XPOST -F "k=v" -F "file=@1112002.png" http://localhost:8080/home |
如果要上传多个文件, 参数名要唯一, 或者改成数组参数名, 例如
| curl -F "f1=@file1.gif" -F "f2=@file2.gif" http://localhost:8080/upload |
或者用数组参数名, 例如
| curl -F "file[]=@file1.gif" -F "file[]=@file2.gif" http://localhost/upload |
提交二进制数据
使用 --data-binary
参数, 不需要指定参数名
| curl -XPOST --data-binary "@asdf.txt" http://localhost:6666 |
| curl -XPOST --header "Content-Type:application/octet-stream" --data-binary @asdf.txt http://localhost:6666 |
全局参数
带错误输出的安静模式 -sS
| -s, |
| Silent or quiet mode. Don't show progress meter or error mes‐ |
| sages. Makes Curl mute. |
| |
| -S, |
| When used with -s it makes curl show an error message if it |
| fails. |
只获取响应头, 用于只需要判断200的场景
| -I, |
| (HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature |
| the command HEAD which this uses to get nothing but the header |
| of a document. When used on an FTP or FILE file, curl displays |
| the file size and last modification time only. |
显示完整请求和响应 -v
例如
| curl -v -F file=@"/home/milton/Downloads/File_28_7.zip" http://127.0.0.1:8768/app/static_file/upload |
| * Trying 127.0.0.1:8768... |
| * TCP_NODELAY set |
| * Connected to 127.0.0.1 (127.0.0.1) port 8768 ( |
| > POST /app/static_file/upload HTTP/1.1 |
| > Host: 127.0.0.1:8768 |
| > User-Agent: curl/7.68.0 |
| > Accept: */* |
| > Content-Length: 291760 |
| > Content-Type: multipart/form-data; boundary=------------------------6e7d753a255e8137 |
| > Expect: 100-continue |
| > |
| * Mark bundle as not supporting multiuse |
| < HTTP/1.1 100 |
| * We are completely uploaded and fine |
| * Mark bundle as not supporting multiuse |
| < HTTP/1.1 200 |
| < Vary: Origin |
| < Vary: Access-Control-Request-Method |
| < Vary: Access-Control-Request-Headers |
| < Content-Type: application/json |
| < Transfer-Encoding: chunked |
| < Date: Thu, 26 Jan 2023 13:16:50 GMT |
| < |
| * Connection |
| {"code":0,"message":"success","data":1} |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
2023-02-12 STM32F407VET6 / BLACK_F407VE开发板间隔0.5秒不断重启
2022-02-12 GRC: 个人信息保护法, 个人隐私, 企业风险合规治理
2021-02-12 OpenWrt的dnsmasq, ipset和iptables配置
2019-02-12 PHP异步扩展Swoole笔记(1)