使用curl 命令模拟POST/GET请求的正确姿势 | 您所在的位置:网站首页 › curl接口测试命令 › 使用curl 命令模拟POST/GET请求的正确姿势 |
开发中常见的调用http的工具除了PostMan外,最常用的就数Curl命令了。 官方文档有非常非常详尽的介绍:https://curl.haxx.se/docs/httpscripting.html 如http协议的相关用法:https://curl.haxx.se/docs/httpscripting.html#The_HTTP_Protocol 最简练的手册:https://curl.haxx.se/docs/manual.html (超级推荐看这个) 本文简单介绍其中常用的一小部分。 一、GET请求如最基本的GET请求: 使用curl发送GET请求的格式为:curl protocol://address:port/url?args 例如: 代码语言:javascript复制 curl https://curl.haxx.se当然如果请求后端的GET接口,也可以得到查询的数据信息。 如果有参数直接拼接在后面即可如: curl ‘http://127.0.0.1:8080/login?name=admin&passwd=12345678’ 二、POST请求POST请求的格式:curl -d "args" protocol://address:port/url 带参数的例子: curl -d "user=admin&passwd=12345678" http://127.0.0.1:8080/login POST数组,比如后端参数为 String[] itemNames,如果想传入a,b,c,d四个元素,这么写: 代码语言:javascript复制curl -d 'itemNames=a&itemNames=b&itemNames=c&itemNames=d' 'http://127.0.0.1:8080/debug/xxxx/yyy' 三、多个请求一起发3.1 多个url也可以一行连发多个Get请求 代码语言:javascript复制curl http://url1.example.com http://url2.example.com多个POST 代码语言:javascript复制curl --data name=curl http://url1.example.com http://url2.example.com3.2 多个请求方法先发HEAD请求然后发GET请求 代码语言:javascript复制curl -I http://example.com --next http://example.com先发POST请求然后发GET请求 代码语言:javascript复制curl -d score=10 http://example.com/post.cgi --next http://example.com/results.html4、表单格式4.1 GET表单代码语言:javascript复制对应的get请求 代码语言:javascript复制curl "http://www.hotmail.com/when/junk.cgi?birthyear=1905&press=OK"4.2 POST表单代码语言:javascript复制请求方式 代码语言:javascript复制 curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi使用URL编码 代码语言:javascript复制curl --data-urlencode "name=I am Daniel" http://www.example.com4.3 文件上传代码语言:javascript复制对应命令: 代码语言:javascript复制curl --form upload=@localfilename --form press=OK [URL]4.4 隐藏域代码语言:javascript复制对应命令 代码语言:javascript复制curl --data "birthyear=1905&press=OK&person=daniel" [URL]5、Cookies基本用法 代码语言:javascript复制curl --cookie "name=Daniel" http://www.example.com更多高级用法参见官方文档: https://curl.haxx.se/docs/httpscripting.html https://curl.haxx.se/docs/ 如果觉得本文对你有帮助,欢迎点赞评论,欢迎关注我,我将努力创作更多更好的文章。 |
CopyRight 2018-2019 实验室设备网 版权所有 |