Request Body and Body Types
What You'll Learn
You'll learn how to attach different types of data to HTTP requests and which HTTP methods allow request bodies.
The Core Concept
When you send data to a server using fetch(), you attach it as a request body. The body can take several forms depending on what you're sending: structured data (JSON), file uploads (FormData), binary data (Blob), or simple text.
Think of the request body as a package you're mailing. You can send a neatly formatted letter (JSON), a box with multiple items (FormData), raw materials (Blob), or a simple note (plain text). The format you choose depends on what the server expects to receive.
Body Types
JSON is the most common format for APIs. You stringify JavaScript objects:
fetch('/api/user', {
method: 'POST',
body: JSON.stringify({ name: 'Alice', age: 30 })
});
FormData mimics HTML form submissions, great for file uploads:
const formData = new FormData();
formData.append('username', 'Alice');
formData.append('avatar', fileBlob);
fetch('/upload', { method: 'POST', body: formData });
Blob sends raw binary data like images or PDFs:
const blob = new Blob(['Hello'], { type: 'text/plain' });
fetch('/data', { method: 'POST', body: blob });
Plain text is the simplest option:
fetch('/message', { method: 'POST', body: 'Hello server' });
Method Restrictions
Only POST, PUT, PATCH, and DELETE typically support request bodies. GET and HEAD requests cannot have bodies—they only fetch data, not send it.
Key Takeaway: Request bodies carry data to servers in formats like JSON, FormData, Blob, or text, but only certain HTTP methods (POST, PUT, PATCH, DELETE) support them.