API 开发神器 - Postman

4,794 阅读5分钟
原文链接: shiyuanjie.cn

Postman helps you develop APIs faster.

豪华午餐

PostmanPostman

Postman

构建、管理、文档化API

  • 在线安装
  • 离线安装
    使用已经安装好的Postman文件夹,机器默认存放目录:/Library/Application\ Support/Google/Chrome/Default/Extensions
    进入chrome://extensions/,选择Load unpacked extensions,加载Postman的文件夹

Newman(我真的不是MP3)Newman(我真的不是MP3)

Newman

Newman is a command-line collection runner forPostman. It allows you to effortlessly run and test a Postman collection directly from the command-line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.

  • 安装: newman:npm install -g newman
  • 功能: 直接运行Postman的接口Collection数据(本地或远程)
  • 运行: 本地Postman保存的Collection内容
➜  Downloads newman -c Demo.postman_collection.json
Iteration 1 of 1
200 1479ms Open the url of Baidu. [GET] http://www.baidu.com
    ✔ Status code is 200
Summary:
Parent                    Pass Count   FailCount
-------------------------------------------------------------
Collection Demo                    1           0
Total                              1           0
  • 运行远程Postman
➜  ~ newman -u https://www.getpostman.com/collections/676a42223e14bf54ad84
Iteration 1 of 1
200 240ms Open the url of Baidu. [GET] http://www.baidu.com
    ✔ Status code is 200
Summary:
Parent                    Pass Count   FailCount
-------------------------------------------------------------
Collection Demo                    1           0
Total                              1           0

InterceptorInterceptor

Interceptor

Postman interceptor brings the power of your Chrome window to Postman! You can set custom headers (including cookies) from within Postman, and view cookies already set on the domain. You can also capture requests being sent from Chrome and import them into Postman. This makes building APIs a breeze!

初级使用

录制API-Interceptor

  • 设置浏览器中的Interceptor:打开开关、设置Filter(仅过滤www.jianshu.com

Chrome中设置InterceptorChrome中设置Interceptor

  • 设置Postman中的Interceptor:打开开关

Postmsn中打开InterceptorPostmsn中打开Interceptor

录制API结果录制API结果

回放API-Postman

  • 选择Postman中的API:Get http://www.jianshu.com/p/4a677c5f79f1

指定API指定API

  • 点击Send,进行回放。可查看到请求的返回状态码200、数据Body
    回放结果回放结果

指定API请求-Postman

1步搞定指定API请求

高级使用

规划管理API-Postman+Collection

将所有的API进行分类管理,如按模块、系统、类型

保存API至Collection

  • 选中需要保存的API,点击请求列表中的Save to collection

Save to collectionSave to collection

  • 可将此API保存至已有的Collection新增Collection

重命令API请求-便于管理和查看

  • 切换到Collections列表,点击API请求的操作区,选择Edit
    Edit-API信息Edit-API信息

常用权限应用尽有-Authorization

Authorization设置Authorization设置

  • Postman在请求时,可指定此次请求Auth方式
  • 可设置Auth中的具体内容,完全自行定义。如设置Basic Auth中的用户名和密码

Basic AuthBasic Auth

完全自定义的头-header

Header设置Header设置

  • 编辑已有Header中的元素
  • 新增Header中请求时,需要的Key:Value
  • 禁用及启用Header中的元素,对于不清楚API请求时,具体哪些是必须要传递的比较有用

完美的数据体-body

Body设置Body设置

  • 在请求需要发送数据体时,可通过设置body内容
  • 支持form-data/x-www-urlencoded/raw/binary,格式的数据

请求前还能做点事-Pre-request Script

Pre-request Script设置Pre-request Script设置

  • 请求发送前,可进行一些脚本设置。如:设置或清除参数、变量

验证API请求结果-Tests

Postman提供了常用的测试功能:返回内容处理、状态码判断、请求超时等。点击后自动添加到Test脚本中
Test命令集合Test命令集合

进阶使用

带你快速走向各个环境-Environment

  • 添加QA环境URL地址变量

添加QA环境的地址变量添加QA环境的地址变量

  • 添加DEV环境的URL地址变量。此时就有两个环境地址

QA、DEV,两个环境QA、DEV,两个环境

  • 修改请求中的URL地址为:p/4a677c5f79f1,切换环境至QA环境

变更URL、切换环境变更URL、切换环境

  • 点击Send,请求QA环境中的数据

真实的请求QA环境真实的请求QA环境

使用Evnironment,可设置环境地址及环境中的不同数据,便于在跨环境后, 相同API可快速使用,减少调整API的成本

分享你的成果-导出/导入 or share

导出-Download

  • Postman中有Download功能(即导出功能),且Download后的可直接导入
  • 可将CollectionEnvironment的数据进行Download
    PS:导出的数据其实是JSON数据格式,可随意

导入-Import

  • 导入所有导出的数据
  • 被导入的数据格式与内容与导出时,完全一致

分享-share

  • share功能需要登录后,才能使用。且shared成功后,对应的链接会保存到用户数据中

share-linkshare-link

  • 点击链接,会直接打开share的API设置内容

导出/导入、share,便于在团队内部协作时使用,API的请求及管理团队化

海量执行你的请求-Runner

当你有大量的API时,肯定想一次执行多个多次执行多个,Postman的Runner可以满足你的需求。

  • 调整API的结构、添加必要的测试验证

添加验证添加验证

  • 打开Runner,设置执行参数:选择要执行的Collection、执行多少次Interation、请求的延迟Delay、数据文件、变量控件

执行参数执行参数

  • Start Test查看运行结果RESULTS

ResultsResults

CI挂起来-Newman

直接使用Newman命令,快速把Postman与CI集成起来(直接使用shell命令,即可)。

newman -c demo.postman_collection --exitCode 1

PS:

  • 3.2.0版本之后,将原来的Jetpack(Runner/Newman)功能免费,以上所提到的功能全部免费使用
  • 本文使用版本为4.2.2

参考