Windows使用curl发送GET、POST请求

本文介绍了如何在Windows上安装和使用curl工具进行GET和POST请求。首先,你需要下载curl for Windows并将其bin目录添加到环境变量。然后,可以使用curl命令获取网页内容或从FTP服务器下载文件。对于GET请求,可以直接在URL中传递参数;对于POST请求,可以使用`-d`或`-F`选项。同时,文章还展示了如何通过Tornado框架创建API来测试这些请求。此外,还提到了美化输出、计时和其他高级用法。最后,推荐了Postman和ApiPost作为API开发和测试的工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

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后的字符串(data)

curl -d "name=Xiao%20ming&phone=13000000000" http://localhost:5555/

-F:模拟表单(form)

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
在这里插入图片描述

参考文献

  1. windows环境下 curl 安装和使用
  2. curl - Tutorial
  3. Formatting cURL Output in the Windows Terminal

转自:Windows使用curl发送GET、POST请求_XerCis的博客-CSDN博客_curl windows 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值