当前位置 : 首页 » 文章分类 :  开发  »  Postman使用笔记

Postman使用笔记

Postman使用笔记

postman flow

https://github.com/postmanlabs/postman-flows/blob/main/tutorials/chaining-requests.md

mac上打开postman看不见窗口

解决方法:
1、Entering full screen(from menu bar or shortcut)
2、Creating a new window. 此时退出全屏就能看到新窗口。
3、全屏关掉老窗口,使用新窗口


Postman 脚本

Postman 脚本中访问变量

脚本中set变量值
pm.environment.set()
pm.globals.set()

脚本中get变量值
pm.variables.get()
pm.environment.get()
pm.globals.get()

var appId = postman.getGlobalVariable(‘app_id’);


Postman 脚本添加 Header

pm.request.headers.upsert("account: admin");
pm.request.headers.upsert("token: xxxx");

Postman 脚本读取及修改请求 body

console.log(`原始请求body: ${pm.request.body.raw}`); // 获取原始请求body json字符串
var requestBody = JSON.parse(pm.request.body.raw); // 解析为json
var name = requestBody.name;  // 获取 body json 的 name 字段
requestBody.name = "new name"; // 修改 name 字段
pm.request.body.raw = JSON.stringify(requestBody); // 更新请求body

使用moment.js格式化时间

Pre-request Script中:

var moment = require('moment')
pm.globals.set("format-datetime", moment().format("MM/DD/YYYY"))

然后引用 {{format-datetime}} 变量即可

How do I format as MM/DD/YYYY in Postman?
https://stackoverflow.com/questions/47355150/how-do-i-format-timestamp-as-mm-dd-yyyy-in-postman

Hold on, wait a moment…
https://dannydainton.com/2018/05/21/hold-on-wait-a-moment/


在Collection或Folder上添加Pre-request scripts

右键 Edit 编辑 Collection 或 Folder,可以看到编辑界面也有类似单个请求的 Pre-request scripts 文本框,把脚本贴到这里,则会对 Collection 或 folder 中的所有请求都起作用。

Pre-request scripts
https://www.getpostman.com/docs/v6/postman/scripts/pre_request_scripts


动态变量 Dynamic variables

: Adds a v4 style guid : Adds the current timestamp : Adds a random integer between 0 and 1000

https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables#dynamic-variables

request中访问变量

在 postman 界面中,可以直接使用 {{variableName}} 来访问变量,会被自动替换为变量值。


Postman中查看请求代码

如果想查看curl纯文本代码格式的请求,可以点击右侧的code按钮,弹出窗口中提供各种语言版本的请求发送代码,非常丰富,如下图:


Postman提供丰富的请求发送代码

Postman中调用返回文件的接口

在postman中测试返回pdf或Excel等文件的接口时,以默认文本格式显示的话就全是乱码。
可以将蓝色的发送请求按钮Send选为Send and Download,则有文件response到来时会打开一个保存文件对话框。

命令行curl调用返回文件的接口

或者不用postman,直接在命令行中使用curl来发送请求,并把响应重定向到一个.pdf文件

curl -X GET \
  'http://localhost:8007/service/v1/pdf/downloads?signature=8e5da6626a555e94ee5dcda' \
  -H 'Authorization: Bearer 2.0deyIQ5nozH/Gm+pKgCKaGlVt0CxFmcEw=' \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Postman-Token: 4e1a3c3b-c0ec-4429-919a-0857cbc6134c' \
  -d 'id=121&name=hahaha&date=20180703' > download.pdf

curl代码可以直接在code中找到,不用自己拼。

上一篇 iTerm2使用笔记

下一篇 Google-Guava 使用笔记

阅读
评论
723
阅读预计3分钟
创建日期 2018-07-03
修改日期 2018-11-14
类别

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论