{
  "version": 12,
  "name": "05_extract_and_chain",
  "description": "Example: pull values out of a response with Extract (from Body, Headers or the Full response, with ordinals) and feed them into the next requests as $variables — a login → token → authorised call chain against dummyjson.com.",
  "globals": [
    {
      "name": "$dummy",
      "value": "https://dummyjson.com"
    },
    {
      "name": "$httpbin",
      "value": "https://httpbin.org"
    },
    {
      "name": "$username",
      "value": "emilys"
    },
    {
      "name": "$password",
      "value": "emilyspass"
    },
    {
      "name": "$token",
      "value": ""
    },
    {
      "name": "$userId",
      "value": ""
    }
  ],
  "globalFunctions": "// Assertion helpers, built on check(label, pass, details).\n// Each records a pass / fail line in the log and the report.\n// A failed one fails its step when the step is done;\n// the code keeps running. Change them or add your own.\n// r defaults to the last response.\n\nfunction assertTrue(value, label = \"is true\") {\n  return check(label, Boolean(value), { actual: value, expected: true });\n}\n\nfunction assertEqual(actual, expected, label = \"equals \" + JSON.stringify(expected)) {\n  return check(label, isEqual(actual, expected), { actual, expected });\n}\n\nfunction assertNotEqual(actual, unexpected, label = \"does not equal \" + JSON.stringify(unexpected)) {\n  return check(label, !isEqual(actual, unexpected), { actual, expected: \"not \" + JSON.stringify(unexpected) });\n}\n\n// text contains a part, or a list contains an item\nfunction assertContains(haystack, needle, label = \"contains \" + JSON.stringify(needle)) {\n  const pass = Array.isArray(haystack) ? haystack.some((x) => isEqual(x, needle)) : String(haystack).includes(needle);\n  return check(label, pass, { actual: haystack, expected: needle });\n}\n\nfunction assertMatch(text, regex, label = \"matches \" + regex) {\n  return check(label, new RegExp(regex).test(String(text)), { actual: text, expected: String(regex) });\n}\n\nfunction assertLessThan(actual, limit, label = \"< \" + limit) {\n  return check(label, Number(actual) < Number(limit), { actual, expected: \"< \" + limit });\n}\n\nfunction assertGreaterThan(actual, limit, label = \"> \" + limit) {\n  return check(label, Number(actual) > Number(limit), { actual, expected: \"> \" + limit });\n}\n\n// expected: 200, \"2xx\", or a list such as [200, 201]\nfunction assertStatus(expected, r = response) {\n  const list = [].concat(expected).map(String);\n  const status = String(r && r.status);\n  const pass = list.some((e) => e === status || (/^[1-5]xx$/i.test(e) && e[0] === status[0]));\n  return check(\"status is \" + list.join(\" or \"), pass, { actual: r && r.status, expected });\n}\n\nfunction assertTimeBelow(ms, r = response) {\n  return check(\"time < \" + ms + \" ms\", Boolean(r) && r.timeMs < ms, { actual: r && r.timeMs, expected: \"< \" + ms });\n}\n\n// without expected, the header only has to exist\nfunction assertHeader(name, expected, r = response) {\n  const value = r ? r.headers[String(name).toLowerCase()] : undefined;\n  if (expected === undefined) return check(\"header \" + name + \" exists\", value !== undefined, { actual: value });\n  return check(\"header \" + name + \" equals \" + expected, String(value) === String(expected), { actual: value, expected });\n}\n\n// e.g. assertJsonPath(\"$.items[0].id\", 42); without expected, the value only has to exist\nfunction assertJsonPath(path, expected, r = response) {\n  const value = jsonPath(path, r);\n  if (expected === undefined) return check(path + \" exists\", value !== undefined, { actual: value });\n  return check(path + \" equals \" + JSON.stringify(expected), isEqual(value, expected), { actual: value, expected });\n}\n\n// schema: a JSON Schema object\nfunction assertSchema(schema, r = response) {\n  const result = validateSchema(r, schema);\n  return check(\"matches the JSON Schema\", result.valid, { error: result.errors || undefined });\n}\n",
  "libraries": [],
  "activeEnvironment": "",
  "auth": {
    "type": "none"
  },
  "settings": {},
  "data": {
    "file": ""
  },
  "cookies": [],
  "tags": [
    "example",
    "extract",
    "chaining"
  ],
  "steps": [
    {
      "body": "{ \"username\": \"$username\", \"password\": \"$password\", \"expiresInMins\": 30 }",
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "exists",
          "path": "$.accessToken",
          "target": "json",
          "value": ""
        }
      ],
      "extract": [
        {
          "after": "\"",
          "before": "\"accessToken\":\"",
          "from": "Body",
          "name": "$token",
          "ordinal": 1
        },
        {
          "after": ",",
          "before": "\"id\":",
          "from": "Body",
          "name": "$userId",
          "ordinal": 1
        }
      ],
      "headers": [
        {
          "name": "Content-Type",
          "value": "application/json"
        }
      ],
      "id": "login",
      "method": "POST",
      "name": "Login and extract the token + user id",
      "type": "request",
      "url": "$dummy/auth/login"
    },
    {
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "$.username",
          "target": "json",
          "value": "emilys"
        }
      ],
      "headers": [
        {
          "name": "Authorization",
          "value": "Bearer $token"
        }
      ],
      "id": "me",
      "method": "GET",
      "name": "Use the token (Bearer header)",
      "type": "request",
      "url": "$dummy/auth/me"
    },
    {
      "checks": [
        {
          "op": "equals",
          "path": "$.username",
          "target": "json",
          "value": "emilys"
        }
      ],
      "id": "user-by-id",
      "method": "GET",
      "name": "Use the extracted id in the URL",
      "type": "request",
      "url": "$dummy/users/$userId"
    },
    {
      "checks": [
        {
          "op": "equals",
          "path": "$.products[1].id",
          "target": "json",
          "value": "2"
        }
      ],
      "extract": [
        {
          "after": ",",
          "before": "{\"id\":",
          "from": "Body",
          "name": "$secondProductId",
          "ordinal": 2
        }
      ],
      "id": "second-match",
      "method": "GET",
      "name": "Extract the 2nd match (ordinal) from a list",
      "params": [
        {
          "name": "limit",
          "value": "3"
        },
        {
          "name": "select",
          "value": "title"
        }
      ],
      "type": "request",
      "url": "$dummy/products"
    },
    {
      "checks": [
        {
          "op": "equals",
          "path": "$.id",
          "target": "json",
          "value": "2"
        }
      ],
      "id": "use-second",
      "method": "GET",
      "name": "Open the 2nd product",
      "type": "request",
      "url": "$dummy/products/$secondProductId"
    },
    {
      "extract": [
        {
          "after": "\r\n",
          "before": "x-request-ref: ",
          "from": "Headers",
          "name": "$requestRef",
          "ordinal": 1
        }
      ],
      "id": "from-headers",
      "method": "GET",
      "name": "Extract from response headers",
      "params": [
        {
          "name": "X-Request-Ref",
          "value": "REF-7781"
        }
      ],
      "type": "request",
      "url": "$httpbin/response-headers"
    },
    {
      "checks": [
        {
          "op": "equals",
          "path": "$.args.ref",
          "target": "json",
          "value": "REF-7781"
        }
      ],
      "id": "echo-ref",
      "method": "GET",
      "name": "Send the header value on",
      "params": [
        {
          "name": "ref",
          "value": "$requestRef"
        }
      ],
      "type": "request",
      "url": "$httpbin/get"
    },
    {
      "checks": [
        {
          "op": "matches",
          "path": "$.uuid",
          "target": "json",
          "value": "^[0-9a-f-]{36}$"
        }
      ],
      "extract": [
        {
          "after": "",
          "before": "",
          "from": "Body",
          "name": "$uuidResponse",
          "ordinal": 1
        }
      ],
      "id": "full-response",
      "method": "GET",
      "name": "Keep the full response in a variable",
      "type": "request",
      "url": "$httpbin/uuid"
    }
  ]
}
