Learn

POST a JSON body

Same as the GET, except you send JSON and expect 201.

Playground

In the playground, pick the POST sample:

SUITE: Users
URL: mock://api

TEST: Create User
  POST: /users
  BODY: {"name": "Jane", "email": "jane@example.com"}
  EXPECT: status == 201

Click Run. The mock answers with 201. You want 1 passed 0 failed.

On the CLI

The hello mock.json only has GET /users, so a POST against it 404s. Use this one instead (in the repo: examples/users/mock.json):

{
  "routes": [
    {
      "method": "GET",
      "path": "/users",
      "status": 200,
      "json": { "data": [{ "id": 7, "name": "Ada" }] }
    },
    {
      "method": "POST",
      "path": "/users",
      "status": 201,
      "json": { "id": 7, "name": "Jane", "email": "jane@example.com" }
    },
    {
      "method": "GET",
      "path": "/users/{id}",
      "status": 200,
      "json": { "id": 7, "name": "Jane", "email": "jane@example.com" }
    }
  ]
}

Then post.sapi:

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

TEST: Create User
  POST: /users
  BODY: {"name": "Jane", "email": "jane@example.com"}
  EXPECT: status == 201

One terminal:

snapapi mock mock.json --port 8765

Another, same folder, venv on:

snapapi post.sapi

A good run:

SnapAPI  Users

  Create User
    POST /users                       201  4ms
  PASS  4ms

  1 passed  0 failed  4ms

If you get expected 201, got 404, the hello mock is still sitting on 8765. Stop it and start this file.