curl 笔记

测试工具

可以用nc快速开启一个端口监听, 用来检查curl请求

# 命令格式 nc -l -p [port], 例如
nc -l -p 8080

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 文件上传

# 格式, -F可以多个
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
Silent or quiet mode. Don't show progress meter or error mes‐
sages. Makes Curl mute.
-S, --show-error
When used with -s it makes curl show an error message if it
fails.

只获取响应头, 用于只需要判断200的场景

-I, --head
(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 (#0)
> 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 #0 to host 127.0.0.1 left intact
{"code":0,"message":"success","data":1}

posted on   Milton  阅读(30)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 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)

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示