Skip to content

Mock server

The mock server answers requests on http://localhost:4010 with canned responses, so a front end or another client can be built and tested without the real API. Only this machine can reach it.

Right-click a folder or .http file and choose Mock server…. It opens in its own tab, where you can change the port and Stop or Reload it. Then point an environment’s baseUrl at http://localhost:4010, and the same .http files work against the mock too.

The Mock server tab listing its routes and the requests it has answeredThe Mock server tab listing its routes and the requests it has answered
The mock server's routes, and a log of the requests it answered.

Each request in the files becomes a route: its method, and its path.

  • A {{variable}} in the path matches any value, so GET {{baseUrl}}/pets/{{id}} answers /pets/7.
  • A base path inside the base URL variable is kept, using the selected environment. With baseUrl set to https://host/api, the route is /api/pets/{{id}}.

A route answers with:

  1. The request’s snapshot, if it has one.
  2. Otherwise, its latest response this session.

Requests with neither are listed as not mocked. Send them, or press Snapshot, then press Reload.

An OpenAPI description the files name (# @reqjet.openapi) adds a route for each operation. It answers with the operation’s first success example, or with an example made up from its schema. A saved response wins over a made-up one for the same route.

  • A path that isn’t mocked gets a 404 that lists the routes.
  • A known path with another method gets a 405.
  • A web app served from this machine, on any port (http://localhost:3000, http://127.0.0.1:5173), can call it from the browser. Pages from other sites can’t read its responses, which can include real responses from your session, and requests addressed to any host other than localhost are refused.

The tab logs every request the mock receives, and what it answered.

reqjet mock runs the same server until you press Ctrl+C:

Terminal window
reqjet mock api/ --port 4010 --env dev # a folder's requests
reqjet mock openapi.yaml # an OpenAPI description on its own
reqjet mock orders.reqjet-workspace # every folder in a workspace