Skip to content
Documentation
jsdoc-open-api
short
Describing Request Body

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:


_10
components:
_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:


_10
POST /survey HTTP/1.1
_10
Host: example.com
_10
Content-Type: application/x-www-form-urlencoded
_10
Content-Length: 28
_10
_10
name=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:


_12
components:
_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
*/