curl辅助调试web程序小技巧

有时候用浏览器调试web程序不方便,有时候用python代码调试web程序又太麻烦,这时候就可以选择使用curl命令来进行调试。

atp或pkg安装curl

apt install curl

pkg install curl

curl命令

最简单的使用方法是curl+web地址,如:

curl baidu.com

输出:

<html>

<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">

</html>

可见是发给我们一个refresh信息,告诉我们要转去www.baidu.com,如果使用curl www.baidu.com 则会看到一大堆输出,也就是百度的主页。

在调试任务中,我们可以使用-X 选项来进行POST PUT等操作,如:

curl -X POST "http://127.0.0.1:8000/user/" -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpass", "email": "[email protected]", "full_name": "Test User" }'
curl -X PUT "http://127.0.0.1:8000/items/5555" -H "Content-Type: application/json" -d '{ "item": { "name": "Foo", "description": "The pretender", "price": 42.0, "tax": 3.2 } }'
 

也就是-X参数接类型,GET PUT POST ,GET是默认参数,可以忽略不写。

然后后面接web网址,如baidu.com 或者测试时的本机地址:127.0.0.1:8000

参数-H后接Header头设置,比如"Content-Type: application/json"

参数-d 后面带发送到服务器的参数,比如:'{"username": "testuser", "password": "testpass", "email": "[email protected]", "full_name": "Test User" }'

(一般GET的,都可以直接在浏览器里实现,在地址栏写地址+参数即可。)

如:

curl -X 'GET' \
  'http://127.0.0.1:8000/users/hello?token=jessica' \
  -H 'accept: application/json'

多个Header信息示例:

curl -X GET "http://127.0.0.1:8000/items/" -H "Content-Type: application/json" -H "x-token:fake-super-secret-token" -H "x-key:fake-super-secret-key"
[{"item":"Portal Gun"},{"item":"Plumbus"}]

猜你喜欢

转载自blog.csdn.net/skywalk8163/article/details/143248782