Windows使用curl发送GET、POST请求

问题描述

Windows无法直接通过curl发送GET、POST请求




安装

  1. 下载 curl for Windows
  2. 将 bin 目录添加到环境变量 Path
  3. 查看版本:curl --version
  4. 测试: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"




美化输出

  1. 下载 jq
  2. 重命名为 jq.exe
  3. 放在 C:\Windows\System32
  4. 调用命令 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/




扩展阅读

  1. Postman——API开发协作平台
    在这里插入图片描述
  2. 需要中文尝试——ApiPost
    在这里插入图片描述




参考文献

  1. windows环境下 curl 安装和使用
  2. curl - Tutorial
  3. Formatting cURL Output in the Windows Terminal
  4. api - cURL POST command line on WINDOWS RESTful service
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XerCis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值