• 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 Permanently</title></head>
      <body bgcolor="white">
      <center><h1>301 Moved Permanently</h1></center>
      <hr><center>nginx</center>
      </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&param2=value2"

    references

  • 相关阅读:
    微信浏览器 video
    css 日常
    input file 上传文件类型控制
    JS的一些日常
    使用canvas时, 如何用相对单位(rem, rpx)来适配不同机型
    微信小程序 textarea的placeholder层级过高 在弹层之上 bug解决方法
    保留两位小数, 不足自动补零
    Java创建线程的两个方法
    android socket 网络数据传输
    java中InputStream中read()与read(byte[] b) 用法介绍
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9421590.html
Copyright © 2020-2023  润新知