The REST API body is the data payload sent by a client to a server within an HTTP request, typically used to create or update a resource. While the URL and headers provide the address and instructions, the body contains the actual information, such as a user's name or a product's price.
Many developers mistakenly believe every API request needs a body, but GET and DELETE requests usually leave the body empty because they identify the resource via the URL alone. Sending a body in a GET request can lead to unpredictable behavior as some servers ignore it entirely.
Rule of thumb: Use the body for data you are sending to the server (POST, PUT, PATCH) and the URL for data you are requesting from the server (GET).
HTTP Methods and Body Usage
The necessity of a request body depends on the HTTP method you choose. Each method signals a different intent to the server, which determines whether the server expects a payload.
POST Requests
A POST request is used to create a new resource. Because you are creating something from scratch, you must provide the details of that resource in the body. For example, if you are creating a new user account, the body would contain the username, email, and password. The server receives this payload and saves it to the database.
PUT and PATCH Requests
These methods are used for updates. A PUT request typically replaces the entire resource. You send the complete updated object in the body; if you omit a field, the server may overwrite that field with a null value. A PATCH request is for partial updates. You only include the specific fields you want to change in the body, leaving the rest of the resource untouched on the server.
GET and DELETE Requests
GET requests retrieve data. The server knows what to send based on the URL path and query parameters. Adding a body to a GET request is technically possible in some protocols but is not standard practice. Similarly, DELETE requests usually target a specific ID in the URL. While some APIs allow a body in a DELETE request to specify multiple IDs, it is rare and often discouraged.
Common Body Formats
The server needs to know how to read the data you send. This is handled by the Content-Type header, which tells the server the format of the body.
JSON (JavaScript Object Notation)
JSON is the industry standard for REST APIs because it is lightweight and easy for both humans and machines to read. It uses key-value pairs. A typical JSON body looks like this:
{
"username": "jdoe",
"email": "jane@example.com",
"age": 30
}
When using JSON, you must set your header to Content-Type: application/json. If you forget this, the server may return a 415 Unsupported Media Type error because it does not know how to parse the raw text.
XML (Extensible Markup Language)
XML was common in older SOAP APIs and is still used in some legacy REST systems. It uses tags similar to HTML. An XML body for the same user would look like this:
<user>
<username>jdoe</username>
<email>jane@example.com</email>
<age>30</age>
</user>
For this format, the header must be Content-Type: application/xml.
Form Data
When a web browser submits a form, it often uses application/x-www-form-urlencoded. This format sends data as a series of key-value pairs separated by ampersands, similar to a URL query string: username=jdoe&email=jane@example.com. This is primarily used for simple web forms rather than complex data structures.
Structuring a Request Body
A well-structured body prevents errors and makes your API predictable. You should follow a consistent schema so the server can validate the incoming data efficiently.
Data Types in the Body
Ensure your values match the types the server expects. Sending a string where an integer is required often results in a 400 Bad Request error. Common types include:
- Strings: Wrapped in double quotes, used for text.
- Numbers: Integers or floats, written without quotes.
- Booleans:
trueorfalse, without quotes. - Arrays: Lists of values wrapped in square brackets
[]. - Objects: Nested sets of key-value pairs wrapped in curly braces
{}.
Handling Nested Data
For complex resources, use nested objects. If you are updating a user's address, do not flatten the fields into address_city and address_zip. Instead, create an address object within the user body. This keeps the data organized and allows the server to process the address as a single entity.
Testing and Debugging the Body
Errors in the request body are a frequent source of API failure. Most of these issues are caused by syntax errors or mismatched data types.
Validating JSON Syntax
A single missing comma or an unclosed curly brace will make the entire body invalid. The server will usually respond with a 400 Bad Request. Use a JSON validator tool to check your syntax before sending the request. Ensure you use double quotes for keys and string values, as single quotes are not valid in the JSON standard.
Interpreting Server Responses
The server's response code tells you if the body was accepted. A 201 Created response means the POST body was processed and a new resource was made. A 400 Bad Request indicates a syntax error or a validation failure in the body. A 422 Unprocessable Entity means the syntax is correct, but the data is logically invalid—for example, an email address missing the @ symbol.
Using API Clients
Tools like Postman or Insomnia allow you to switch between different body types easily. To test a body, go to the Body tab, select "raw", and choose "JSON" from the dropdown menu. This automatically sets the Content-Type header for you, reducing the chance of a configuration error. Labels in these tools may differ slightly by version.
Body Size and Performance
The amount of data you put in a body can affect the speed and stability of your application. Sending excessively large payloads can lead to timeouts or server crashes.
Payload Limits
Most servers have a maximum request body size, often set to 1MB or 10MB by default. If you exceed this limit, the server will return a 413 Payload Too Large error. To handle larger files, such as images or PDFs, do not send them as base64 strings inside a JSON body. This increases the file size by about 33%.
Efficient Data Transfer
To keep bodies small, only send the fields that are necessary. In a PATCH request, sending only the changed field instead of the entire object reduces bandwidth and processing time. If you must send large amounts of data, consider using multipart/form-data, which allows you to send binary files and text fields in a single request without the overhead of JSON encoding.
Compression
For very large bodies, you can use GZIP compression. The client compresses the body before sending it and adds the header Content-Encoding: gzip. The server then decompresses the data upon arrival. This significantly reduces the time spent transmitting data over the network, especially for mobile users on slow connections.