问题描述
Windows无法直接通过curl发送GET、POST请求
安装
- 下载 curl for Windows
- 将 bin 目录添加到环境变量 Path
- 查看版本:
curl --version
- 测试:
curl www.baidu.com
基本使用
获取百度首页
curl http://www.baidu.com/
获取funet ftp服务器上的README
curl ftp://ftp.funet.fi/README
更多用法查看curl - Tutorial
GET请求
用Tornado构建API用于GET、POST请求测试
import tornado.web
import tornado.ioloop
class MainHandler(tornado.web.RequestHandler):
def get(self):
name = self.get_argument('name', default='')
phone = self.get_argument('phone', default='')
self.write('GET {} {}'.format(name, phone))
def post(self):
name = self.get_argument('name', default='')
phone = self.get_argument('phone', default='')
self.write('POST {} {}'.format(name, phone))
if __name__ == '__main__':
print('curl "http://localhost:5555/?name=Xiao%20ming&phone=13000000000"') # GET
print('curl -d "name=Xiao%20ming&phone=13000000000" http://localhost:5555/') # POST
print('curl -F "name=Xiao ming" -F "phone=13000000000" http://localhost:5555/') # POST
app = tornado.web.Application([
(r'/', MainHandler),
])
app.listen(5555)
tornado.ioloop.IOLoop.current().start()
GET请求
curl "http://localhost:5555/?name=Xiao%20ming&phone=13000000000"
POST请求
-d
:接受urlencode后的字符串
curl -d "name=Xiao%20ming&phone=13000000000" http://localhost:5555/
curl -X POST http://127.0.0.1:5000/pets -H "Content-Type: application/json" --data-raw "{\"name\":\"Butterfly\", \"category\":\"cat\"}"
-F
:模拟表单
curl -F "name=Xiao ming" -F "phone=13000000000" http://localhost:5555/
POST发文件
curl -X POST "http://xxx" --form "file=@a.png"
美化输出
- 下载 jq
- 重命名为
jq.exe
- 放在
C:\Windows\System32
中 - 调用命令
echo {"a": 1} | jq
计时
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.kklinux.com"
下载文件
wget -O result.pdf http://127.0.0.1:5000/download/
扩展阅读
- Postman——API开发协作平台
- 需要中文尝试——ApiPost