Describing Responses
Describing Responses
An API specification needs to specify the responses
for all API operations.
Each operation must have at least one response defined, usually a successful response.
A response is defined by its HTTP status code and the data returned in the response body and/or headers.
Here is a minimal example:
_10/**_10 * GET /ping_10 * @response 200 - OK_10 * @responseContent {string} 200.text/plain_10 */
Response media types
An API can respond with various media types.
JSON is the most common format for data exchange, but not the only one possible.
To specify the response media types, use @responseContent
.
_16/**_16 * GET /users_16 * @summary Get all users_16 * @response 200 - A list of users_16 * @responseContent {ArrayOfUsers} 200.application/json_16 * @responseContent {ArrayOfUsers} 200.application/json_16 * @responseContent {string} 200.text/plain_16 */_16_16// This operation returns image_16/**_16 * GET /logo_16 * @summary Get the logo image_16 * @response 200 - Logo image in PNG format_16 * @responseContent {binary} 200.image/png_16 */
HTTP status codes
Each response definition starts with a status code, such as 200 or 404. An operation typically returns one successful status code and one or more error statuses. To define a range of response codes, you may use the following range definitions: 1XX, 2XX, 3XX, 4XX, and 5XX. If a response range is defined using an explicit code, the explicit code definition takes precedence over the range definition for that code. Each response status requires a description. For example, you can describe the conditions for error responses. Markdown (CommonMark (opens in a new tab)) can be used for rich text representation.
_10/**_10 * @response 200 - OK_10 * @response 400 - Bad request. User ID must be an integer and larger than 0._10 * @response 401 - Authorization information is missing or invalid._10 * @response 404 - A user with the specified ID was not found._10 * @response 5XX - Unexpected error._10 */
An API specification does not necessarily need to cover all possible HTTP response codes, since they may not be known in advance. However, it’s expected to cover successful responses and any known errors. By "known errors" we mean, for example, a 404 Not Found response for an operation that returns a resource by ID, or a 400 Bad Request response in case of invalid operation parameters.
Response body
A response body can define:
- An
object
or anarray
— typically used with JSON and XML APIs - A primitive data type such as a
number
orstring
– used for plain text responses - A file – (see below)
Objects can be defined in components
:
_11components:_11 schemas:_11 User:_11 type: object_11 properties:_11 id:_11 type: integer_11 description: The user ID._11 username:_11 type: string_11 description: The user name.
And be used with:
_10/**_10 * @response 200 - A User object_10 * @responseContent {User} 200.application/json_10 */
Response that returns a file
An API operation can return a file, such as an image or PDF.
If the response returns the file alone,
you would typically use the binary
type and specify the appropriate media type for the response content:
_10/**_10 * GET /report_10 * @summary Returns the report in the PDF format_10 * @response 200 - A PDF file_10 * @responseContent {binary} 200.application/pdf_10 */
Files can also be embedded into, say, JSON or XML as a base64-encoded string. In this case, you would use something like:
_10/**_10 * GET /users/me_10 * @summary Returns user information_10 * @response 200 - A JSON object containing user name and avatar_10 * @responseContent {User} 200.application/json_10 */
_11components:_11 schemas:_11 User:_11 type: object_11 properties:_11 username:_11 type: string_11 avatar: # <-- image embedded into JSON_11 type: string_11 format: byte_11 description: Base64-encoded contents of the avatar image
Empty response body
Responses, such as 204 No Content, have no body.
To indicate the response body is empty, do not specify @responseContent
:
_10/**_10 * @response 204 - The resource was deleted successfully._10 */
Response headers
Responses from an API can include custom headers to provide additional information on the result of an API call. For example, a rate-limited API may provide the rate limit status via response headers as follows:
_10HTTP 1/1 200 OK_10X-RateLimit-Limit: 100_10X-RateLimit-Remaining: 99_10X-RateLimit-Reset: 2016-10-12T11:00:00Z_10_10{ ... }
You can define custom headers
for each response as follows:
_10/**_10 * GET /ping_10 * @summary Checks if the server is alive._10 * @response 200 - OK_10 * @responseHeader {integer} 200.X-RateLimit-Limit - Request limit per hour._10 * @responseHeader {integer} 200.X-RateLimit-Remaining - The number of requests left for the time window._10 * @responseHeader {date-time} 200.X-RateLimit-Reset - The UTC date/time at which the current rate limit window resets._10 */
, currently, OpenAPI Specification does not permit to define common response headers for different response codes or different API operations. You need to define the headers for each response individually.
Response examples
Examples can be defined in components
:
_10components:_10 examples:_10 Jessica:_10 value:_10 id: 10_10 name: Jessica Smith_10 Ron:_10 value:_10 id: 11_10 name: Ron Stewart
And be used as:
_10/**_10 * POST /users_10 * @summary Adds a new user_10 * @response 200 - OK_10 * @responseContent {User} 200.application/json_10 * @responseExample {Jessica} 200.application/json.Jessica_10 * @responseExample {Ron} 200.application/json.Ron_10 */
Default response
Sometimes, an operation can return multiple errors with different HTTP status codes, but all of them have the same response structure:
_10/**_10 * @response 200 - Success_10 * @responseContent {User} 200.application/json_10 *_10 * @response 400 - Bad request_10 * @responseContent {Error} 400.application/json_10 *_10 * @response 404 - Not found_10 * @responseContent {Error} 404.application/json_10 */
You can use the default response to describe these errors collectively, not individually. "Default" means this response is used for all HTTP codes that are not covered individually for this operation.
_10/**_10 * @response 200 - Success_10 * @responseContent {User} 200.application/json_10 *_10 * @response default - Unexpected error_10 * @responseContent {Error} default.application/json_10 */
Reusing responses
Responses can be defined in components
to be reused elsewhere.
The following response definition:
_26components:_26 responses:_26 NotFound:_26 description: The specified resource was not found_26 content:_26 application/json:_26 schema:_26 $ref: "#/components/schemas/Error"_26 Unauthorized:_26 description: Unauthorized_26 content:_26 application/json:_26 schema:_26 $ref: "#/components/schemas/Error"_26 schemas:_26 # Schema for error response body_26 Error:_26 type: object_26 properties:_26 code:_26 type: string_26 message:_26 type: string_26 required:_26 - code_26 - message
Can be reused as:
_21/**_21 * GET /users_21 * @summary Gets a list of users._21 *_21 * @response 200 - OK_21 * @responseContent {ArrayOfUsers} 200.application/json_21 *_21 * @responseComponent {Unauthorized} 401_21 */_21_21/**_21 * GET /users/{id}_21 * @summary Gets a user by ID._21 *_21 * @response 200 - OK_21 * @responseContent {User} 200.application/json_21 *_21 * @responseComponent {Unauthorized} 401_21 *_21 * @responseComponent {NotFound} 404_21 */