Describing Request Body
Describing Request Body
Request bodies are typically used with "create" and "update" operations (POST, PUT, PATCH). For example, when creating a resource using POST or PUT, the request body usually contains the representation of the resource to be created.
It’s important to unlike parameters, request bodies are optional by default.
To mark the body as required, use @bodyRequired
.
_11/**_11 * POST /pets_11 * @summary Add a new pet_11 * @bodyDescription Optional description in *Markdown*_11 * @bodyContent {Pet} application/json_11 * @bodyContent {Pet} application/xml_11 * @bodyContent {PetForm} application/x-www-form-urlencoded_11 * @bodyContent {string} text/plain_11 * @bodyRequired_11 * @response 201 - Created_11 */
@bodyContent
allows wildcard media types.
For example, image/*
represents all image types; */*
represents all types and is functionally equivalent to application/octet-stream
.
Specific media types have preference over wildcard media types when interpreting the spec, for example, image/png
> image/*
> */*
.
Note:
*/*
must be escaped in comments with*\/*
.
_10// Can be image/png, image/svg, image/gif, etc._10/**_10 * PUT /avatar_10 * @summary Upload an avatar_10 * @bodyContent {binary} image/*_10 * @bodyRequired_10 * @response 201 - Created_10 */
_10// Can be anything._10/**_10 * PUT /file_10 * @summary Upload any file_10 * @bodyContent {binary} *\/*_10 * @bodyRequired_10 * @response 201 - Created_10 */
Reusable request bodies
Request bodies can be defined in components
to be reused elsewhere.
The following request body definition:
_10components:_10 requestBodies:_10 PetBody:_10 description: A JSON object containing pet information_10 required: true_10 content:_10 application/json:_10 schema:_10 $ref: "#/components/schemas/Pet"
Can be reused as:
_11/**_11 * POST /pets_11 * @summary Add a new pet_11 * @bodyComponent {PetBody}_11 */_11_11/**_11 * PUT /pets/{petId}_11 * @summary Update a pet_11 * @bodyComponent {PetBody}_11 */
Form data
The term "form data" is used for the media types application/x-www-form-urlencoded
and multipart/form-data
,
which are commonly used to submit HTML forms.
application/x-www-form-urlencoded
is used to send ASCII text data as key=value pairs. The payload format is similar to query parameters.multipart/form-data
allows submitting binary data as well as multiple media types in a single message (for example, image, and JSON). Each form field has its own section in the payload with internal HTTP headers.multipart
requests are commonly used for file uploads.
To illustrate form data, consider an HTML POST form:
_10<form action="http://example.com/survey" method="post">_10 <input type="text" name="name" />_10 <input type="number" name="fav_number" />_10 <input type="submit" />_10</form>
This form POSTs data to the form’s endpoint:
_10POST /survey HTTP/1.1_10Host: example.com_10Content-Type: application/x-www-form-urlencoded_10Content-Length: 28_10_10name=Amy+Smith&fav_number=42
Form data is defined in components
and modeled using a type: object
schema where the object properties represent the form fields:
_12components:_12 schemas:_12 Survey:_12 type: object_12 properties:_12 name: # <!--- form field name_12 type: string_12 fav_number: # <!--- form field name_12 type: integer_12 required:_12 - name_12 - fav_number
It can be used with:
_10/**_10 * POST /survey_10 * @bodyContent {Survey} application/x-www-form-urlencoded_10 * @bodyRequired_10 */