99人参与 • 2024-08-06 • stm32
在开发过程中调试接口,一般都会使用postman。
其中有几个变量可能是好几个接口共用的,就会出现频繁手动复制(ctrl+c)、粘贴(ctrl+v)的情况。
这个过程得非常留意,生怕复制错了,或删减了某些东西,导致接口报错。
总是这样复制就显得非常繁琐和麻烦了。
那有没有办法可以让postman自动设置变量呢 ?
这就是本文要介绍的,postman动态设置变量。
本文内容中,使用的环境如下
postman版本
: postman v11.1.14
windows10/windows11
首先来介绍下文本演示的接口 。
这里用到的是阿里的百炼大模型assistant api
的接口,这是用来调用阿里大模型的接口。
附上文档链接 : https://help.aliyun.com/document_detail/2741924.html?spm=a2c4g.2741923.0.0.15f64739xkecig
注意这里需要先申请一个dashscope-api-key
,才可以调用下面的这些接口。
代码示例
curl --location 'https://dashscope.aliyuncs.com/api/v1/threads' \
--header 'content-type: application/json' \
--header 'authorization: bearer <your-dashscope-api-key>' \
--data '""'
返回的结果
{
"id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
"object": "thread",
"created_at": 1711448377850,
"metadata": {},
"request_id": "dd9489ec-dbdb-95d4-9ff8-cfe29b61db27"
}
代码示例
curl --location 'https://dashscope.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages' \
--header 'content-type: application/json' \
--header 'authorization: bearer <your-dashscope-api-key>' \
--data '{
"role": "user",
"content": "你是谁",
"metadata": {}
}'
返回结果
{
"id": "message_f1933671-19e1-4162-ad25-7326165123e1",
"object": "thread.message",
"created_at": 1711508433283,
"thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
"assistant_id": "",
"run_id": "",
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "你是谁",
"annotations": []
}
}
],
"file_ids": [],
"metadata": {},
"from": "",
"name": "",
"plugin_call": {},
"tool_calls": [],
"status": {},
"request_id": "b3ad40b9-f052-9665-a064-dab11c34625f"
}
点击send
,看下返回的结果
{
"id": "thread_9e70b593-3e47-4d61-9adb-4253a937d09c",
"object": "thread",
"created_at": 1717823196058,
"metadata": {},
"request_id": "44cc2792-d063-9578-bd6a-183698123456"
}
注意这里的id
,也就是thread_id
,在下一个接口中会用到。
这里的url中,有一个thread_9e70b593-3e47-4d61-9adb-4253a937d09c
,也就是thread_id
,是从上一个接口里取的,这里我们直接将其复制过来。
{
"role": "user",
"content":"87787 加 788988737. 结果是多少?",
"metadata": {}
}
点击send
,看下返回的结果
{
"id": "message_9d4e6396-863e-4219-83ff-ab0fe5144b61",
"object": "thread.message",
"created_at": 1717823800660,
"thread_id": "thread_9e70b593-3e47-4d61-9adb-4253a937d09c",
"incomplete_details": {},
"completed_at": null,
"incomplete_at": null,
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "87787 加 788988737. 结果是多少?",
"annotations": []
}
}
],
"assistant_id": "",
"run_id": "",
"file_ids": [],
"metadata": {},
"status": "",
"name": "",
"plugin_call": {},
"tool_calls": [],
"request_id": "2511defb-1ead-9bd7-8080-fed150123456"
}
上文中,我们通过ctrl+c
和ctrl+v
的方式,来赋值thread_id
。
那么如何让postman
动态设置thread_id
,从而不用每次去手动复制了。
首先,我们要知道postman中,有一个scripts
,可以写javascript
的代码。
pre-request
表示在这个http请求执行前,会先调用。
post-response
表示在这个http请求执行后,会调用。
这里,我们在pre-request
中,打印准备创建thread
console.log("准备创建thread")
在post-response
中,打印创建thread完毕
console.log("创建thread完毕")
具体显示日志的地方在postman
左下角的console
中
现在我们执行这个http
请求,会发现打印如下日志了
我们再来看下接口返回的数据
{
"id": "thread_9e70b593-3e47-4d61-9adb-4253a937d09c",
"object": "thread",
"created_at": 1717823196058,
"metadata": {},
"request_id": "44cc2792-d063-9578-bd6a-183698123456"
}
这里我们需要取到id
,并动态设置成全局变量,那需要怎么做呢 ?
,在新版responsebody
已被弃用postman
中,通过pm.response.text()
,我们可以获取到response
中的文本内容。
pm.response.text()
然后通过json.parse()
可以将json
字符串解析为一个json
对象
var jsonobj = json.parse(pm.response.text())
然后就可以通过jsonobj.id
获取到id
字段了
var thread_id = jsonobj.id
console.log("thread_id:"+thread_id)
最后,调用pm.globals.set()
将其设置到全局变量里就好
pm.globals.set("thread_id",thread_id)
使用{{}}
可以获取到对应的全局动态变量
比如,我们这里就可以用{{thread_id}}
来替代url
中的thread_9e70b593-3e47-4d61-9adb-4253a937d09c
了
运行结果如下所示
到此,就完成了postman
动态配置变量的操作,不用再每次复制粘贴变量值了。
参考文章
use scripts to add logic and tests to postman requests
2024最新版postman接口测试教程
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论