默认curl使用GET方式请求数据,这种方式下直接通过URL传递数据
可以通过 --data/-d 方式指定使用POST方式传递数据
1 # GET
2 curl https://api.github.com/user?access_token=XXXXXXXXXX
3
4 # POST
5 curl -d "param1=value1¶m2=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")
}'
package main
import "fmt"
func main() {
fmt.Println("aaaaaaafffffhhhhh")
}'