Skip to content

Build the response

Before you begin

Role
Editor or higher.
You need
  • A mock to change, or a project to create one in

A mock’s response is a status code, headers and a body, set under What it answers in the mock editor. A mock may hold several named responses; this page edits the one that is open, and Choose the response that answers covers the rest. This page builds the answer to POST /orders: 201 with a Location header and the new order as JSON.

  1. Open the mock from the project’s Mocks tab.

  2. Enter the Status code, such as 201. Its meaning, Created, appears beneath it.

  3. Select the body’s format for Response body: JSON, XML, HTML or Text. The format sets the Content-Type header to match.

  4. Select Add a response header.

  5. Enter the header’s name, such as Location. Common header names are suggested as you type.

  6. Enter the header’s value, such as /orders/8821.

  7. Write the body in the Response body editor:

    { "id": 8821, "status": "pending", "total": 42.5 }
  8. Select Format in the editor’s toolbar to indent the body.

  9. Select Save mock.

Status code takes any code from 100 to 599. Beneath it, the editor names a registered code by its meaning, such as Created for 201 or Service Unavailable for 503. A code with no registered meaning, such as 299, is sent all the same, with nothing beneath it.

Format Content-Type it sets The editor
JSON application/json Highlights and formats JSON
XML application/xml Highlights and formats XML
HTML text/html Highlights and formats HTML
Text text/plain Takes the body as plain text

The editor follows the Content-Type header too: set it by hand to application/problem+json, for example, and the body is still treated as JSON. When the body is not valid JSON or XML, Format leaves it as it is and says “Cannot format the body”, with the reason.

When the project’s Response templating is on, the body and the header values can use Handlebars expressions such as {{request.path}}, filled in each time the mock answers. See Write dynamic responses with templates.

An HTML mock on a shared domain, such as orders-api.mockhub.dev, opens in a browser as a page, but its scripts and forms do not run. Code that calls it, such as fetch or a test’s HTTP client, reads it exactly as written. A page your end-to-end tests drive in a browser, such as a mocked sign-in or payment step, needs a project on a custom domain, where nothing is added to the answer.

A Set-Cookie header with a Domain other than the project’s own hostname arrives without its Domain on a shared domain, so the cookie stays on the project’s hostname. Pages and cookies on a shared domain has both rules.

After Save mock, the mock answers matching requests with the status, headers and body you set. The Mocks tab shows the status code on the mock’s row.

  • A body is text. A mock cannot answer with binary content, such as an image or a ZIP file.
  • A body arrives at once, after any delay. To send it in paced pieces, stream the response; to answer as a model’s API, mock an LLM API.
  • MockHub sets the headers that describe the connection itself: Content-Length, Content-Encoding, Transfer-Encoding, Connection and the like. A value the mock gives them is dropped.
  • A response larger than the plan’s traffic response limit fails with 502; see How MockHub handles a request. Every mock’s body, with its version history, also counts towards the workspace’s configuration size.
  • The editor does not refuse a body that is not valid JSON. The mock sends it as written.
  • On a shared domain, every answer carries Content-Security-Policy: sandbox, and a cookie’s Domain is kept only when it names the project’s own hostname. A mock cannot turn either off; see Serving HTML pages.

Report a problem with this page