Skip to main content

HTTP with mock://

Goal

Learn what an HTTP block publishes, without needing a network connection or credentials. Time: 5 minutes.

A url beginning with mock:// returns a canned 200 response. It makes no network request. Direct URLs are accepted only for mock://. To call a real API, you use a saved endpoint, which is covered in the next lesson.

schema: 1
name: HTTP mock
id: http-mock
blocks:
  - id: ping
    kind: http
    method: GET
    url: mock://users
    outputs:
      type: object
      properties:
        path: { type: string }
        mock: { type: boolean }
  - id: read
    kind: code
    language: python
    uses: [steps.ping.status, steps.ping.path, steps.ping.body]
    body: |
      import json, os
      d = json.load(open(os.environ["TERMORQ_INPUT"], encoding="utf-8"))["steps"]["ping"]
      print(json.dumps({"line": f"{d['status']} {d['path']} ({len(d['body'])} bytes)"}))
    outputs:
      type: object
      properties:
        line: { type: string }

What HTTP publishes

PropertyTypeNotes
statusnumberAlways available. A non-success status fails the block (HTTP 404.)
bodystringThe response body, capped at 4,096 bytes in the record
top-level JSON keysvariesWhen the response is a JSON object. Declare the ones you need in outputs. The mock body is {"ok":true,"mock":true,"method":"GET","path":"users"}

Templates work in url too, for example url: "mock://users/{{ loop.index }}".

Check your run

read publishes line: "200 users (…bytes)".

Exercise

Change the method to POST and publish the method property from the mock response.

Details

Add method: { type: string } to ping.outputs.properties, then read steps.ping.method in a later block. It is "POST".