• CURL命令 : GET、POST请求、文件下载等常用命令



    1. curl介绍

    curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。

    2. 直接发送请求 (Get)

    默认是Get请求

    curl URL 
    
    • 1

    相当于发送一个GET请求,curl默认就是发送GET请求

    测试:

    curl https://jsonplaceholder.typicode.com/posts/1
    
    {
      "userId": 1,
      "id": 1,
      "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
      "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    3. 发送Post请求

    curl -X -POST URL 
    或者 
    curl -XPOST URL
    
    • 1
    • 2
    • 3

    测试

    curl -XPOST  https://jsonplaceholder.typicode.com/posts
    
    {
    	"id": 101
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    3.1 Post请求带参数

    curl -XPOST URL -d '{"key1":"value1","key2":"value2"}'
    
    • 1

    测试

    curl -XPOST https://jsonplaceholder.typicode.com/posts -d '{"name":"张三", "age":"20"}'
    
    {
      "{\"name\":\"张三\", \"age\":\"20\"}": "",
      "id": 101
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4. Put请求和Delete请求

    curl -XPUT URL 和 curl -XDELETE URL
    带参数的
    curl -XPUT URL -d '{}' 和 curl -XDELETE URL -d '{}'
    
    • 1
    • 2
    • 3

    测试put

    curl -XPUT https://jsonplaceholder.typicode.com/posts/1 -d '{"name":"李四", "age":"10"}'
    
    输出
    {
      "{\"name\":\"李四\", \"age\":\"10\"}": "",
      "id": 1
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5. 携带首部信息

    curl -XPOST URL -H '' -H '' //可以携带多个首部信息
    
    • 1

    测试

    curl -XPOST  https://jsonplaceholder.typicode.com/posts -H 'Content-type:application/json' 
    			 -H 'A-cc-dept:ap-aption/json' -d '{"title":"张三"}'
    
    {
      "title": "张三",
      "id": 101
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6. 获取响应的所有的首部信息

    curl -I URL  // 使用大写I获取
    
    • 1

    测试

    curl -I https://jsonplaceholder.typicode.com/posts
    
    HTTP/2 200
    date: Sun, 14 Aug 2022 10:02:42 GMT
    content-type: application/json; charset=utf-8
    x-powered-by: Express
    x-ratelimit-limit: 1000
    x-ratelimit-remaining: 999
    x-ratelimit-reset: 1652476599
    vary: Origin, Accept-Encoding
    access-control-allow-credentials: true
    cache-control: max-age=43200
    pragma: no-cache
    expires: -1
    x-content-type-options: nosniff
    etag: W/"6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM"
    via: 1.1 vegur
    cf-cache-status: HIT
    age: 12762
    expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=t%2BwhJvMXQq7z8IdGOyEt7%2FgeW%2BRZy4MnLJ32vPvCHZA916xs20rYcy9zfbaAI9tGQ6372ztL5%2F1lXkg1bObDaorK9Zh7DoArRzxNfx%2FkJky3APOvo7Y1ryTscGC6dicHh2UO3JVdtbimr5Hyw8U%2F"}],"group":"cf-nel","max_age":604800}
    nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
    server: cloudflare
    cf-ray: 73a8db3e5b0d7b59-LAX
    alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    7. 文件下载

    curl -O URL 直接下载到当前文件夹里面 下载之前记得cd切换文件夹
    
    • 1
    curl -o https://profile.csdnimg.cn/F/0/6/1_m0_37989980
    
    输出:
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   107  100   107    0     0    512      0 --:--:-- --:--:-- --:--:--   532
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    8. 显示底层连接信息

    curl -v URL //会显示握手信息等
    
    • 1

    9. 通过代理访问

    curl --proxy 协议://用户名:密码@代理地址:端口 URL
    curl --proxy "http://egg:123@127.0.0.1:1234" URL
    
    • 1
    • 2

    10. 通过不同协议访问

    curl -u 用户名:密码 -O ftp://server/aaa.avi 下载
    curl -u 用户名:密码 -T 文件 ftp://server
    
    • 1
    • 2
  • 相关阅读:
    群组分析方法
    通往优秀软件架构师之路:掌握技术核心,修炼基础原理【文中送书,十本任选】
    Web3到底是什么?和区块链有啥关系?
    html实训大作业《简单的学生网页作业源码 基于html css javascript红色文化传媒网站(20页)》
    【分享】小红书采集图片下载到本地
    姓祝男孩名字简单大气
    对透传 Attributes 的理解
    基于CNN的动物识别系统[完整实战源码]
    Android绘制的Window和View
    react-query
  • 原文地址:https://blog.csdn.net/m0_37989980/article/details/126334010