Describing Request Body
The POST, PUT and PATCH requests can have the request body (payload), such as JSON or XML data. In Swagger terms, the request body is called a body parameter. There can be only one body parameter, although the operation may have other parameters (path, query, header).
Note: The payload of the application/x-www-form-urlencoded
and multipart/form-data
requests is described by using form parameters, not body parameters.
The body parameter is defined in the operation’s parameters section and includes the following:
in: body
schema
that describes the body data type and structure. The data type is usually an object, but can also be a primitive (such as a string or number) or an array.- Optional
description
. - The payload name. It is required but ignored (it is used for documentation purposes only).
Object Payload (JSON, etc.)
Many APIs transmit data as an object, such as JSON. schema
for an object should specify type: object
and properties for that object. For example, the POST /users
operation with this JSON body:
can be described as:
Note: The name of the body parameter is ignored. It is used for documentation purposes only. For a more modular style, you can move the schema definitions to the global definitions
section and refer to them by using $ref
:
Primitive Body
Want to POST/PUT just a single value? No problem, you can define the body schema type as a primitive, such as a string or number. Raw request:
Swagger definition:
Did not find what you were looking for? Ask the community
Found a mistake? Let us know