Last updated

Responses Map

The Responses Map describes the possible responses to an HTTP request. It is declared in the Operation Object. The documentation is expected to cover at least one successful case.

responses:
  '200':
    # Response Object | Reference Object
Excerpt from the OpenAPI 3.1 specification about responses

A container for the expected responses of an operation. The container maps a HTTP response code to the expected response.

The documentation is not necessarily expected to cover all possible HTTP response codes because they may not be known in advance. However, documentation is expected to cover a successful operation response and any known errors.

The default MAY be used as a default response object for all HTTP codes that are not covered individually by the Responses Object.

The Responses Object MUST contain at least one response code, and if only one response code is provided it SHOULD be the response for a successful operation call.

This object MAY be extended with Specification Extensions.

Fixed Fields
Field NameTypeDescription
defaultResponse Object | Reference ObjectThe documentation of responses other than the ones declared for specific HTTP response codes. Use this field to cover undeclared responses.
Patterned Fields
Field PatternTypeDescription
HTTP Status CodeResponse Object | Reference ObjectAny HTTP status code can be used as the property name, but only one property per code, to describe the expected response for that HTTP status code. This field MUST be enclosed in quotation marks (for example, "200") for compatibility between JSON and YAML. To define a range of response codes, this field MAY contain the uppercase wildcard character X. For example, 2XX represents all response codes between [200-299]. Only the following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX. If a response is defined using an explicit code, the explicit code definition takes precedence over the range definition for that code.

Visuals

The following examples use a minimal Response Object with a description. The Response Object topic covers the response object in depth.

default

The default can be used to cover undeclared responses. The default response is not required and used less frequently.

If there are no successful responses declared in the responses map, then the default is highlighted as successful in green.

responses:
  default:
    description: Default response
  '400':
    description: Bad Request
  '409':
    description: Conflict
  '503':
    description: Service unavailable

default is green

If there is at least one successful response declared, then the default is highlighted similarly to an error in red.

responses:
  '200':
    description: OK
  default:
    description: Default response

default is red

Successful response

The responses MUST contain one successful response. As mentioned above, if there is a default and no other successful responses, then it is assumed to be the successful response.

Otherwise, the successful responses are in the 200-series from 200 to 299. The common responses are:

Successful codeDescription
200OK
201Created
202Accepted
204No Content

It is also possible to use 2XX to indicate any successful response.

responses:
  '200':
    description: OK
  '201':
    description: Created
  '202':
    description: Accepted

200, 201, 202 green

responses:
  2XX:
    description: Success

200, 201, 202 green

Redirects

Redirects are in the 300-series (300-399). 3XX is used to indicate any redirect response.

The common responses are:

Redirect codeDescription
301Moved Permanently
302Found
303See Other
responses:
  '200':
    description: OK
  '303':
    description: See Other

redirect

Client errors

Client errors are in the 400-series (400-499). It is possible to use 4XX to indicate any client error.

The common client errors are:

Error codeDescription
400Moved Permanently
401Unauthorized
403Forbidden
404Not Found
409Conflict
422Unprocessable Content
429Too Many Requests
responses:
  '200':
    description: OK
  '400':
    description: Bad Request
  '409':
    description: Conflict

client errors

Server errors

Server errors are in the 500-series (500-599). They can be represented with 5XX to indicate any server error.

Server errors are usually not described in API definitions.

The common server errors are:

Error codeDescription
500Internal Server Error
502Bad Gateway
503Service Unavailable
504Gateway Timeout
responses:
  '200':
    description: OK
  '503':
    description: Service Unavailable

server errors

Informational response

While rarely defined in APIs, it is possible to describe informational responses which are in the 100-series (100-199). Use 1XX to describe the entire range of informational responses.

The common informational responses are:

Informational codeDescription
100Continue
101Switching Protocols
responses:
  '100':
    description: Continue
  '200':
    description: OK

informational continue

Types

  • NamedResponses
  • Responses
  • Response
const Responses: NodeType = {
  properties: { default: 'Response' },
  additionalProperties: (_v: any, key: string) =>
    responseCodeRegexp.test(key) ? 'Response' : undefined,
};

const Response: NodeType = {
  properties: {
    description: { type: 'string' },
    headers: mapOf('Header'),
    content: 'MediaTypesMap',
    links: mapOf('Link'),
  },
  required: ['description'],
};