cURL(wget)—— 测试 RESTful 接口及模拟 GET/POST/PUT/DELETE/OPTIONS 请求

cURL 是一个简单的 http 命令行工具。与最优秀的 Unix 工具一样,在设计之时,cURL 是个小型程序,功能十分专一,而且是故意为之,仅用于访问 http 服务器。(在 Linux 中,可以使用包管理器轻易安装,apt-get install curl/yum install curl)。

  • curl:默认会将下载文件输出到stdout
  • $ curl https://api.github.com
    • github api 是超媒体 api。
    • 从上述命令行的输出可以看出,其输出响应包含一个映射,列出了接下来可能会发起请求的地址,
      • code_search_url键对应的 url 显然用于在 github 中搜索代码;
  • -I/–head:只打印返回头信息,而不下载远程文件;

0. wget

  • -q(–quiet):turn off wget’s output;(对于 curl 则是 –silent)

1. 查看网页源码

  • $ curl www.sina.com(仅仅是查看)

    <html>
    <head><title>301 Moved Permanentlytitle>head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanentlyh1>center>
    <hr><center>nginxcenter>
    body>
    html>
  • 如果要将该网页保存下来,可以使用 -o 参数,相当于 wget 命令了:

    $ curl -o [filename] www.sina.com

2. 模拟GET/POST/PUT/DELETE/OPTIONS 请求

http://ju.outofmemory.cn/entry/84875

常用参数:

  • -X/–request,后可跟:GET/POST/PUT/DELETE/OPTIONS

    curl -X GET "http://www.rest.com/api/users"
    curl -X POST "http://www.rest.com/api/users"
    curl -X PUT "http://www.rest.com/api/users"
    curl -X DELETE "http://www.rest.com/api/users"
  • -H/–header,指定 request 的头部

    curl -v -i -H "Content-Type: application/json" http://www.example.com/users
  • -i/–include:显示 response 的 header
  • -d/–data:指定请求的参数;

    curl -X POST -d "param1=value1¶m2=value2"

references

  • curl网站开发指南

你可能感兴趣的:(cURL(wget)—— 测试 RESTful 接口及模拟 GET/POST/PUT/DELETE/OPTIONS 请求)