Learn

SAVE an id, then GET it

Creates usually return an id. SAVE keeps it so the next request in the same test can use ${userId}.

Playground

Pick SAVE then GET in the playground:

SUITE: Users
URL: mock://api

TEST: Create then fetch
  POST: /users
  BODY: {"name": "Jane", "email": "jane@example.com"}
  EXPECT: status == 201
  SAVE: userId FROM $.id
  GET: /users/${userId}
  EXPECT: status == 200
  EXPECT: json $.email == "jane@example.com"

FROM $.id is JSONPath — the id field on the POST response. After SAVE, ${userId} is that number. Click Run. Look for saved userId= and 1 passed.

On the CLI

Same mock as POST (needs POST /users and GET /users/{id}). In the repo: examples/users/mock.json and examples/users/save.sapi.

SUITE: Users
URL: http://127.0.0.1:8765

TEST: Create then fetch
  POST: /users
  BODY: {"name": "Jane", "email": "jane@example.com"}
  EXPECT: status == 201
  SAVE: userId FROM $.id
  GET: /users/${userId}
  EXPECT: status == 200
  EXPECT: json $.email == "jane@example.com"
snapapi mock mock.json --port 8765   # leave running
snapapi save.sapi

A good run:

SnapAPI  Users

  Create then fetch
    POST /users                       201  4ms
    saved userId=7
    GET /users/7                      200  1ms
  PASS  5ms

  1 passed  0 failed  5ms

POST created the user, SAVE kept 7, GET fetched /users/7, email matched. One test, two calls.

Undefined variable ${userId} means SAVE didn’t run, or the JSON doesn’t have id at the top (on a real API it’s often $.data.id).