告别 Postman、ARC、DHC,拥抱 curl

2016-09-17 · 992 chars · 5 min read

今年一直在公司折腾 nodejs,实实在在的体会了一把“随便玩玩”和“真正投产”的区别。在各种环境,各种防火墙,各种权限中对程序做验证,少不了 curl 这个神器,只要掌握几个简单的命令,完全可以抛弃 Postman、ARC、DHC、REST Console 等“好用”的 chrome 扩展。

在介绍前,我需要先做两点说明:

  1. 下面的例子中会使用 httpbin.org,httpbin 提供客户端测试 http 请求的服务,非常好用,具体可以查看他的网站。
  2. 大部分没有使用缩写形式的参数,例如我使用--request 而不是-X ,这是为了好记忆。

下面开始简单介绍几个命令:

直接获取(GET)一个 url#

直接以个 GET 方式请求一个 url,输出返回内容:

curl httpbin.org

返回

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv='content-type' value='text/html;charset=utf8'>
  <meta name='generator' value='Ronn/v0.7.3 (http://github.com/rtomayko/ronn/tree/0.7.3)'>
  <title>httpbin(1): HTTP Client Testing Service</title>
  <style type='text/css' media='all'>
  /* style: man */
  body#manpage {margin:0}
  .mp {max-width:100ex;padding:0 9ex 1ex 4ex}
  .mp p,.mp pre,.mp ul,.mp ol,.mp dl {margin:0 0 20px 0}
  .mp h2 {margin:10px 0 0 0}
......

post,put 等#

使用--request 指定请求类型,--data 指定数据,例如:

curl httpbin.org/post --request POST --data "name=keenwon&website=http://keenwon.com"

返回:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "name": "keenwon",
    "website": "http://keenwon.com"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "39",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "json": null,
  "origin": "180.157.173.141",
  "url": "http://httpbin.org/post"
}

这个返回值是 httpbin 输出的,可以清晰的看出我们发送了什么数据,非常实用。

form 表单提交#

form 表单提交使用--form ,使用@ 指定本地文件,例如我们提交一个表单,有字段 name 和文件 f:

curl httpbin.org/post --form "name=keenwon" --form "f=@/Users/keenwon/Desktop/a.txt"

返回:

{
  "args": {},
  "data": "",
  "files": {
    "f": "hello, world!"
  },
  "form": {
    "name": "keenwon"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "293",
    "Content-Type": "multipart/form-data; boundary=------------------------5bff0fea23249059",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "json": null,
  "origin": "180.157.173.141",
  "url": "http://httpbin.org/post"
}

显示头信息#

使用--include 在输出中包含头信息,使用--head  只返回头信息,例如:

curl httpbin.org/post --include --request POST --data "name=keenwon"

返回:

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 17 Sep 2016 07:46:12 GMT
Content-Type: application/json
Content-Length: 364
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "name": "keenwon"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "12",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "json": null,
  "origin": "180.157.173.141",
  "url": "http://httpbin.org/post"
}

再例如,只显示头信息的话:

curl httpbin.org --head

返回:

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 17 Sep 2016 07:49:52 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12150
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

详细显示通信过程#

使用--verbose 显示通信过程,例如:

curl httpbin.org/post --verbose --request POST --data "name=keenwon"

返回:

*   Trying 23.22.14.18...
* Connected to httpbin.org (23.22.14.18) port 80 (#0)
> POST /post HTTP/1.1
> Host: httpbin.org
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 12
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 12 out of 12 bytes
< HTTP/1.1 200 OK
< Server: nginx
< Date: Sat, 17 Sep 2016 07:59:31 GMT
< Content-Type: application/json
< Content-Length: 364
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
<
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "name": "keenwon"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "12",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "json": null,
  "origin": "180.157.173.141",
  "url": "http://httpbin.org/post"
}
* Connection #0 to host httpbin.org left intact

设置头信息#

使用--header 设置头信息,httpbin.org/headers 会显示请求的头信息,我们测试下:

curl httpbin.org/headers --header "a:1"

返回:

{
  "headers": {
    "A": "1",
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  }
}

同样的,可以使用--header 设置 User-Agent 等。

Referer 字段#

设置 Referer 字段很简单,使用--referer ,例如:

curl httpbin.org/headers --referer http://keenwon.com

返回:

{
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "Referer": "http://keenwon.com",
    "User-Agent": "curl/7.43.0"
  }
}

使用--cookie 来设置请求的 cookie,例如:

curl httpbin.org/headers --cookie "name=keenwon;website=http://keenwon"

返回:

{
  "headers": {
    "Accept": "*/*",
    "Cookie": "name=keenwon;website=http://keenwon",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  }
}

自动跳转#

使用--location 参数会跟随链接的跳转,例如:

curl httpbin.org/redirect/1 --location

httpbin.org/redirect/1 会 302 跳转,所以返回:

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "origin": "180.157.173.141",
  "url": "http://httpbin.org/get"
}

http 认证#

当页面需要认证时,可以使用--user ,例如:

curl httpbin.org/basic-auth/keenwon/123456 --user keenwon:123456

返回:

{
  "authenticated": true,
  "user": "keenwon"
}
赞赏

微信