curl工具发起http请求

curl命令是一个利用URL规则在命令行下工作的文件传输工具。
我们可以利用它测试restful API

官方文档:https://curl.haxx.se/docs/httpscripting.html

1 GET 请求

官方sample: curl https://curl.haxx.se

curl -X GET "http://xxx.xx.xx.x:xxx/v2/users/message?id=xxxxxxxxxxxx&userid=xxxxxxxxxx"  --header "Authorization:Bearer <access_token>"

2 POST 请求

官方sample: curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi

2.1 x-www-form-urlencoded类型的数据

 curl --data "birthyear=1905&press=%20OK%20"  http://www.example.com/when.cgi

上面–data必须自己处理编码比如其中的空格等。

带编码版本的post参数:

 curl --data-urlencode "name=I am Daniel" http://www.example.com

2.2 application/json类型的数据:

curl -X POST "http://xxx.xx.xx.x:xxxx/v2/users/friends"  --header "Authorization:Bearer <access_token>" --header "Content-Type:application/json" --data '{"memoname":"xxxx","id":"[email protected]"}'

3 DELETE请求

curl -X DELETE "http://xxx.xx.xx.x:xxx/v2/users/message?id=xxxxxxxxxxxx" --header "Authorization:Bearer  <access_token>" 

4 PUT请求

curl -X PUT "http://xxx.xx.xx.x:xxxx/v2/users/friends" --header "Authorization:Bearer  <access_token>" --header "Content-Type:application/json" --data '{"memoname":"xxxx","id":"[email protected]"}'

猜你喜欢

转载自blog.csdn.net/m0_37263637/article/details/79807379