curl 一个强大的http请求工具

来源:互联网 发布:家用配电箱品牌知乎 编辑:程序博客网 时间:2024/06/06 04:12
默认curl使用GET方式请求数据,这种方式下直接通过URL传递数据

可以通过 --data/-d 方式指定使用POST方式传递数据

1 # GET2 curl https://api.github.com/user?access_token=XXXXXXXXXX3 4 # POST5 curl -d "param1=value1&param2=value" https://api.github.com

-i/--include 输出时包括protocol头信息

-c/--cookie-jar <file> 操作结束后把cookie写入到这个文件中

 curl -c cookie.txt -d "username=1234&password=1234" http://localhost:2345/service/manager/login

-b/--cookie <name=string/file> cookie字符串或文件读取位置 

-H "Content-Type: application/json, xtoken: test_token_123" 指定http头内容

curl -b cookie.txt http://localhost:1234/service/edit/schmo/list?page=1\&perpage=5\&report_status=1

除了使用GET和POST协议外,还可以通过 -X 选项指定其它协议

curl -I -X DELETE https://api.github.cim

默认情况下,通过POST方式传递过去的数据中若有特殊字符,首先需要将特殊字符转义在传递给服务器端,如value值中包含有空格,则需要先将空格转换成%20,如:

1 curl -d "value%201" http://hostname.com

在新版本的CURL中,提供了新的选项 --data-urlencode,通过该选项提供的参数会自动转义特殊字符。

1 curl --data-urlencode "value 1" http://hostname.com


用postman提交post表单请求时,表单字符串无法转义(换行),可以使用命令
curl http://localhost:8080/RunCode/go -d 'code=
package main
import "fmt"
func main() {
    fmt.Println("aaaaaaafffffhhhhh")
}'



1 0
原创粉丝点击