# Responses

Source: https://apigenie.pl/docs/reference/server/responses
Part of the reference of [apigenie-server](https://apigenie.pl/docs/reference/server.md).
Its other pages: [Scenarios and extensions](https://apigenie.pl/docs/reference/server/scenarios.md), [Scripts](https://apigenie.pl/docs/reference/server/scripts.md), [Document store](https://apigenie.pl/docs/reference/server/store.md), [Several services](https://apigenie.pl/docs/reference/server/services.md), [Validation and security](https://apigenie.pl/docs/reference/server/validation.md), [Contract testing (validating proxy)](https://apigenie.pl/docs/reference/server/contract-testing.md), [Session recording and replay](https://apigenie.pl/docs/reference/server/recording.md), [AsyncAPI](https://apigenie.pl/docs/reference/server/asyncapi.md), [Protobuf / gRPC](https://apigenie.pl/docs/reference/server/grpc.md), [GraphQL](https://apigenie.pl/docs/reference/server/graphql.md), [Configuration](https://apigenie.pl/docs/reference/server/configuration.md), [Dashboard and admin API](https://apigenie.pl/docs/reference/server/dashboard.md), [TLS](https://apigenie.pl/docs/reference/server/tls.md).

How the mock answers a request: where the paths are served, which status, body and headers it picks, how a request asks for a particular answer, and how the network is made slow or unreliable on purpose. [How the mock picks a response](/docs/concepts/how-the-mock-picks-a-response) walks through these rules with a document to try them on.

- **Routing.** Paths are served under the path of the document's first server (`/v1` for
  `https://api.example.com/v1`) and as they are. Literal paths win over templated ones
  (`/pets/mine` over `/pets/{id}`). An unknown path is answered with 404, an unknown method with
  405 and an `Allow` header, both as [problem details](https://www.rfc-editor.org/rfc/rfc9457).
  OpenAPI 3.2's `query` method and the methods of `additionalOperations` (`COPY`, ...) are routed
  like the others.
- **Status.** The lowest 2xx response, else `2XX`, else the lowest declared, else `default`.
- **Body.** Content negotiation follows `Accept` (406 when nothing fits). Without an `Accept`, or
  with `*/*`, the response takes the media type of the request's body when it has it: a SOAP 1.2
  request (`application/soap+xml`) to an operation answering both SOAP versions gets a SOAP 1.2
  envelope. In the default static
  mode the media type's `example`, its first named example, or the schema's example answer; what
  has none is generated from the schema. Generated values respect types, the formats of the
  OpenAPI format registry (`uuid`, `date-time`, `int8`, `decimal`, `http-date`, ...; see
  [Formats](/docs/reference/cli/formats)), patterns, lengths, ranges, `enum`, `const`, `allOf`,
  `oneOf`, `readOnly` and `writeOnly`. A value for `application/xml`, `text/xml` or a `+xml`
  type is written as XML (see [XML bodies](/docs/reference/server/validation#xml-bodies)); a string example is sent as it is.
- **Headers.** Response headers are filled in from their examples or schemas.
  `X-Apigenie-Mock-Operation` and `X-Apigenie-Mock-Source` (`example:cat`, `generated`,
  `scenario:outage`, `stub:getPet-1`) say where a response came from.
- **Stubs.** `--stubs DIR` (or `stubs:` in the configuration) answers with recorded exchanges
  (see [Record and replay](/docs/reference/server/contract-testing)) before the document's examples:
  a request's `Prefer` and the rules of scenarios and overrides still come first.

## Choosing a response per request

The `Prefer` header (or query parameters, which are removed before validation) asks for a
particular answer:

| `Prefer` | Query | Effect |
| --- | --- | --- |
| `code=404` | `__code=404` | That status (500 when the operation doesn't declare it). |
| `example=cat` | `__example=cat` | That named example. |
| `dynamic=true` | `__dynamic=true` | A generated body. |
| `delay=1500` | `__delay=1500` | Answer after 1.5 seconds. |
| `drop=reset` | | Reset the connection (`close`, `timeout` and `truncate` too). |
| `scenario=outage` | | Apply that scenario to this request. |

## Network simulation

Latency (fixed, uniform, normal or log-normal), jitter, dropped connections (reset, closed without
an answer, never answered, or truncated bodies) and bandwidth limits apply to every response, an
operation's, a scenario's or one request's.
