POST 请求

  • 参考

  • 参数

    • -X, –request

      • 可选值: POST, GET, …
    • -F, –form

      • 背后机制:设置 Content-Type:multipart/format
      • 格式: -F 'key=value'
      • 用途: 发送请求字段
    • -d, –data

      • 背后机制:设置 Content-Type=application/x-www-form-urlencoded
      • 格式:
    • -d 'key=value' -d 'key01=value01' –> 一次制定一个字段
    • -d 'key01=value01&key02=value02' –> 多个字段
    • -H, –header

      • 制定 http header, Content-Type
1
2
3
curl -X POST -H "Content-Type: application/json" \
     -d '{"name": "linuxize", "email": "linuxize@example.com"}' \
     https://example/contact

上传文件

使用 -F 和 @ 形式

1
curl -X POST -F 'image=@/home/user/Pictures/wallpaper.jpg' http://example.com/upload

下载文件

-O 使用远程文件名

–remote-name

1
curl -O https://website.com/myfile.txt # 下载成为 myfile.txt

-C - 继续下载,断点续传

–continue-at -

1
curl -C - -O https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/11.2/FreeBSD-11.2-RELEASE-amd64-disc1.iso

http 测试

-I 只获取 http 头

GET 请求

传递 query 参数

无参数

1
curl -X GET http://httpbin.org/get
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/8.5.0",
    "X-Amzn-Trace-Id": "Root=1-68abbf89-6566cdff1583b88b21ef069d"
  },
  "origin": "60.174.203.34",
  "url": "http://httpbin.org/get"
}

添加 query 参数 ?key=value 方式

1
curl -X GET 'http://httpbin.org/get?firstname=Lucy&name=Lucy%20Smith&id=3'
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "args": {
    "firstname": "Lucy",
    "id": "3",
    "name": "Lucy Smith"
  },
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/8.5.0",
    "X-Amzn-Trace-Id": "Root=1-68abc4b7-758f72131b3e79331cc540d7"
  },
  "origin": "60.174.203.34",
  "url": "http://httpbin.org/get?firstname=Lucy&name=Lucy Smith&id=3"
}

使用 -G –data-urlencode 'key=value' 格式

特点:

  • 适合特别长的文本
  • 可以不用转义空格
1
2
curl -G --data-urlencode 'name=Lucy Smith' --data-urlencode 'id=3' 'http://httpbin.org/get'
#curl -X GET --data-urlencode 'name=Lucy' --data-urlencode 'id=3' 'http://httpbin.org/get' # 注意这种不加 `-G`的格式会导致 --data-urlencode 传递的数据被放入 body 中
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "args": {
    "id": "3",
    "name": "Lucy Smith"
  },
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/8.5.0",
    "X-Amzn-Trace-Id": "Root=1-68abc484-2c03a36619a0acdb0cbdd079"
  },
  "origin": "60.174.203.34",
  "url": "http://httpbin.org/get?name=Lucy+Smith&id=3"
}

响应 Response

相应头 response header

方法:

  • -i/--include: 添加相应头同时保留 response body

    1
    
    curl -i 'http://httpbin.org/get'

    HTTP/1.1 200 OK Date: Mon, 25 Aug 2025 02:11:11 GMT Content-Type: application/json Content-Length: 253 Connection: keep-alive Server: gunicorn/19.9.0 Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true

    { "args": {}, "headers": { "Accept": "/", "Host": "httpbin.org", "User-Agent": "curl/8.5.0", "X-Amzn-Trace-Id": "Root=1-68abc63f-3eb40e4c568af5f417055be9" }, "origin": "60.174.203.34", "url": "http://httpbin.org/get" }