跳到主要内容

Apipost内置变量

提示

APIPOST内置系统变量包含 request 对象和 response 对象。

request 对象

一个请求的所有请求参数组成的对象。我们在预执行脚本可以打印查看并使用该变量。

image.png

如上图所示,该对象的结构如下:

{
"id": "221b31d1-f2fa-4fcb-8ca7-b37b0094a034",
"name": "演示接口",
"description": "",
"url": "https://demo-api.apipost.cn/api/get.php",
"method": "POST",
"timeout": 0,
"contentType": "application/json",
"request_headers": {
"Cache-Control": "no-cache",
"content-type": "application/json",
"token": "afsjdnk23j4nk2",
"content-token": "content-token22222",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"User-Agent": "PostmanRuntime-ApipostRuntime/1.1.0",
"Connection": "keep-alive"
},
"request_querys": {
"remale": "1"
},
"mode": "json",
"request_bodys": "",
"request_variables": {},
"data": "",
"headers": {
"Cache-Control": "no-cache",
"content-type": "application/json",
"token": "afsjdnk23j4nk2",
"content-token": "content-token22222",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"User-Agent": "PostmanRuntime-ApipostRuntime/1.1.0",
"Connection": "keep-alive"
},
"body": {
"mode": "raw",
"raw": ""
}
}

我们可以通过此对象,在脚本中获取当前的API请求的请求参数信息,从而实现达到我们的一些目的。

response 对象

一个请求所有的响应参数组成的对象。只能用于后执行脚本(发送了才有响应)。我们在后执行脚本可以打印查看并使用该变量。

image.png

该对象的对应结构如下:

{
"target_id": "8ad7d334-5181-43d7-a476-f4659eebed21",
"responseTime": 285,
"responseSize": 0.31,
"resposneAt": "01:43:44",
"netWork": {
"agent": "Desktop-Agent",
"address": {
"remote": {
"address": "122.114.104.58",
"family": "IPv4",
"port": 443
}
}
},
"status": "OK",
"code": 200,
"timingPhases": {
"wait": 0.48262500017881393,
"dns": 58.130667000077665,
"tcp": 35.37691699992865,
"firstByte": 109.4776669992134,
"download": 1.4745420003309846,
"total": 285.3353340001777,
"secureHandshake": 80.39291600044817
},
"resHeaders": {
"date": "Thu, 15 Sep 2022 17:43:44 GMT",
"server": "Apache",
"x-powered-by": "PHP/7.1.3",
"access-control-allow-origin": "*",
"access-control-allow-method": "POST,GET,PUT,DELETE,OPTIONS,OPTION",
"access-control-allow-methods": "POST,GET,PUT,DELETE,OPTIONS,OPTION",
"access-control-allow-headers": "Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin,Authorization",
"set-cookie": [
"cookie-test1-folder=B; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/echo; domain=echo.apipost.cn",
"cookie-test1=0; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn",
"cookie-test2=0; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn",
"cookie-test3=%25E4%25BD%25A0%25E5%25A5%25BD; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn",
"cookie-test4=%E4%BD%A0%E5%A5%BD; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=.apipost.cn",
"cookie-test5=nihao; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=apipost.cn",
"cookie-test6=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain=echo.apipost.cn",
"cookie-test7=0; expires=Thu, 15-Sep-2022 17:13:44 GMT; Max-Age=-1800; path=/; domain=echo.apipost.cn",
"cookie-test8=renge",
"httponly-cookie=httponly-value; HttpOnly"
],
"strict-transport-security": "max-age=63072000; includeSubdomains; preload",
"vary": "Accept-Encoding",
"content-encoding": "gzip",
"content-length": "321",
"keep-alive": "timeout=5, max=100",
"connection": "Keep-Alive",
"content-type": "application/json;charset=utf-8"
},
"headers": {
"date": "Thu, 15 Sep 2022 17:43:44 GMT",
"server": "Apache",
"x-powered-by": "PHP/7.1.3",
"access-control-allow-origin": "*",
"access-control-allow-method": "POST,GET,PUT,DELETE,OPTIONS,OPTION",
"access-control-allow-methods": "POST,GET,PUT,DELETE,OPTIONS,OPTION",
"access-control-allow-headers": "Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin,Authorization",
"set-cookie": [
"cookie-test1-folder=B; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/echo; domain=echo.apipost.cn",
"cookie-test1=0; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn",
"cookie-test2=0; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn",
"cookie-test3=%25E4%25BD%25A0%25E5%25A5%25BD; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn",
"cookie-test4=%E4%BD%A0%E5%A5%BD; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=.apipost.cn",
"cookie-test5=nihao; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=apipost.cn",
"cookie-test6=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain=echo.apipost.cn",
"cookie-test7=0; expires=Thu, 15-Sep-2022 17:13:44 GMT; Max-Age=-1800; path=/; domain=echo.apipost.cn",
"cookie-test8=renge",
"httponly-cookie=httponly-value; HttpOnly"
],
"strict-transport-security": "max-age=63072000; includeSubdomains; preload",
"vary": "Accept-Encoding",
"content-encoding": "gzip",
"content-length": "321",
"keep-alive": "timeout=5, max=100",
"connection": "Keep-Alive",
"content-type": "application/json;charset=utf-8"
},
"header": [
{
"key": "date",
"value": "Thu, 15 Sep 2022 17:43:44 GMT"
},
{
"key": "server",
"value": "Apache"
},
{
"key": "x-powered-by",
"value": "PHP/7.1.3"
},
{
"key": "access-control-allow-origin",
"value": "*"
},
{
"key": "access-control-allow-method",
"value": "POST,GET,PUT,DELETE,OPTIONS,OPTION"
},
{
"key": "access-control-allow-methods",
"value": "POST,GET,PUT,DELETE,OPTIONS,OPTION"
},
{
"key": "access-control-allow-headers",
"value": "Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin,Authorization"
},
{
"key": "set-cookie",
"value": "cookie-test1-folder=B; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/echo; domain=echo.apipost.cn"
},
{
"key": "set-cookie",
"value": "cookie-test1=0; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn"
},
{
"key": "set-cookie",
"value": "cookie-test2=0; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn"
},
{
"key": "set-cookie",
"value": "cookie-test3=%25E4%25BD%25A0%25E5%25A5%25BD; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=echo.apipost.cn"
},
{
"key": "set-cookie",
"value": "cookie-test4=%E4%BD%A0%E5%A5%BD; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=.apipost.cn"
},
{
"key": "set-cookie",
"value": "cookie-test5=nihao; expires=Thu, 15-Sep-2022 18:43:44 GMT; Max-Age=3600; path=/; domain=apipost.cn"
},
{
"key": "set-cookie",
"value": "cookie-test6=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain=echo.apipost.cn"
},
{
"key": "set-cookie",
"value": "cookie-test7=0; expires=Thu, 15-Sep-2022 17:13:44 GMT; Max-Age=-1800; path=/; domain=echo.apipost.cn"
},
{
"key": "set-cookie",
"value": "cookie-test8=renge"
},
{
"key": "set-cookie",
"value": "httponly-cookie=httponly-value; HttpOnly"
},
{
"key": "strict-transport-security",
"value": "max-age=63072000; includeSubdomains; preload"
},
{
"key": "vary",
"value": "Accept-Encoding"
},
{
"key": "content-encoding",
"value": "gzip"
},
{
"key": "content-length",
"value": "321"
},
{
"key": "keep-alive",
"value": "timeout=5, max=100"
},
{
"key": "connection",
"value": "Keep-Alive"
},
{
"key": "content-type",
"value": "application/json;charset=utf-8"
}
],
"fitForShow": "Monaco",
"resMime": {
"ext": "json",
"mime": "application/json"
},
"rawCookies": [
{
"name": "cookie-test1-folder",
"value": "B",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/echo",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test1",
"value": "0",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test2",
"value": "0",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test3",
"value": "%E4%BD%A0%E5%A5%BD",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test4",
"value": "你好",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": ".apipost.cn"
},
{
"name": "cookie-test5",
"value": "nihao",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": "apipost.cn"
},
{
"name": "cookie-test6",
"value": "deleted",
"expires": "1970-01-01T00:00:01.000Z",
"maxAge": 0,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test7",
"value": "0",
"expires": "2022-09-15T17:13:44.000Z",
"maxAge": -1800,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test8",
"value": "renge"
},
{
"name": "httponly-cookie",
"value": "httponly-value",
"httpOnly": true
}
],
"cookies": {
"cookie-test1-folder": "B",
"cookie-test1": "0",
"cookie-test2": "0",
"cookie-test3": "%E4%BD%A0%E5%A5%BD",
"cookie-test4": "你好",
"cookie-test5": "nihao",
"cookie-test6": "deleted",
"cookie-test7": "0",
"cookie-test8": "renge",
"httponly-cookie": "httponly-value"
},
"rawBody": "{\"errcode\":0,\"errstr\":\"success\",\"post\":{\"body\":\"test\"},\"get\":[],\"request\":{\"body\":\"test\"},\"file\":[],\"put\":\"\",\"header\":{\"User-Agent\":\"ApiPOST Runtime +https:\\/\\/www.apipost.cn\",\"Accept\":\"*\\/*\",\"Accept-Encoding\":\"gzip, deflate, br\",\"Connection\":\"keep-alive\",\"Hello\":\"Tom\",\"Host\":\"echo.apipost.cn\",\"Content-Type\":\"multipart\\/form-data; boundary=--------------------------078348776592732067426026\",\"Content-Length\":\"163\"},\"cookie\":[],\"bigint\":248963637882912768}",
"base64Body": "data:application/json;base64,eyJlcnJjb2RlIjowLCJlcnJzdHIiOiJzdWNjZXNzIiwicG9zdCI6eyJib2R5IjoidGVzdCJ9LCJnZXQiOltdLCJyZXF1ZXN0Ijp7ImJvZHkiOiJ0ZXN0In0sImZpbGUiOltdLCJwdXQiOiIiLCJoZWFkZXIiOnsiVXNlci1BZ2VudCI6IkFwaVBPU1QgUnVudGltZSAraHR0cHM6XC9cL3d3dy5hcGlwb3N0LmNuIiwiQWNjZXB0IjoiKlwvKiIsIkFjY2VwdC1FbmNvZGluZyI6Imd6aXAsIGRlZmxhdGUsIGJyIiwiQ29ubmVjdGlvbiI6ImtlZXAtYWxpdmUiLCJIZWxsbyI6IlRvbSIsIkhvc3QiOiJlY2hvLmFwaXBvc3QuY24iLCJDb250ZW50LVR5cGUiOiJtdWx0aXBhcnRcL2Zvcm0tZGF0YTsgYm91bmRhcnk9LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0wNzgzNDg3NzY1OTI3MzIwNjc0MjYwMjYiLCJDb250ZW50LUxlbmd0aCI6IjE2MyJ9LCJjb29raWUiOltdLCJiaWdpbnQiOjI0ODk2MzYzNzg4MjkxMjc2OH0=",
"stream": {
"type": "Buffer",
"data": [
123,
...
125
]
},
"raw": {
"status": 200,
"responseTime": 285,
"type": "json",
"responseText": "{\"errcode\":0,\"errstr\":\"success\",\"post\":{\"body\":\"test\"},\"get\":[],\"request\":{\"body\":\"test\"},\"file\":[],\"put\":\"\",\"header\":{\"User-Agent\":\"ApiPOST Runtime +https:\\/\\/www.apipost.cn\",\"Accept\":\"*\\/*\",\"Accept-Encoding\":\"gzip, deflate, br\",\"Connection\":\"keep-alive\",\"Hello\":\"Tom\",\"Host\":\"echo.apipost.cn\",\"Content-Type\":\"multipart\\/form-data; boundary=--------------------------078348776592732067426026\",\"Content-Length\":\"163\"},\"cookie\":[],\"bigint\":248963637882912768}"
},
"json": {
"errcode": 0,
"errstr": "success",
"post": {
"body": "test"
},
"get": [

],
"request": {
"body": "test"
},
"file": [

],
"put": "",
"header": {
"User-Agent": "ApiPOST Runtime +https://www.apipost.cn",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Hello": "Tom",
"Host": "echo.apipost.cn",
"Content-Type": "multipart/form-data; boundary=--------------------------078348776592732067426026",
"Content-Length": "163"
},
"cookie": [

],
"bigint": 248963637882912770
},
"resCookies": [
{
"name": "cookie-test1-folder",
"value": "B",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/echo",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test1",
"value": "0",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test2",
"value": "0",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test3",
"value": "%E4%BD%A0%E5%A5%BD",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test4",
"value": "你好",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": ".apipost.cn"
},
{
"name": "cookie-test5",
"value": "nihao",
"expires": "2022-09-15T18:43:44.000Z",
"maxAge": 3600,
"path": "/",
"domain": "apipost.cn"
},
{
"name": "cookie-test6",
"value": "deleted",
"expires": "1970-01-01T00:00:01.000Z",
"maxAge": 0,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test7",
"value": "0",
"expires": "2022-09-15T17:13:44.000Z",
"maxAge": -1800,
"path": "/",
"domain": "echo.apipost.cn"
},
{
"name": "cookie-test8",
"value": "renge"
},
{
"name": "httponly-cookie",
"value": "httponly-value",
"httpOnly": true
}
],
"filename": "response_8ad7d334-5181-43d7-a476-f4659eebed21.json"
}

response.raw:原始响应数据

调用示例:

response.raw.status //响应状态码(200、301、404等)
response.raw.responseTime //响应时间(毫秒)
response.raw.type //响应类型(json等)
response.raw.responseText //响应文本

response.json:json格式的响应数据

image.png

调用示例如上面示例:

response.json.data.token //也可以 response.json.data["token"]

response.headers:响应头

image.png

调用示例:

response.headers.server //也可以 response.headers["server"]

response.cookies :响应cookie

image.png

调用示例:

response.cookies.PHPSESSION //也可以 response.cookies["PHPSESSION"]