The Request Object and Request Configuration
What you'll learn: How to use the Request constructor to build detailed, reusable request configurations for the Fetch API.
Understanding the Request Object
So far, you've been passing URLs and simple options directly to fetch(). The Request object gives you a more structured way to create and configure HTTP requests. Think of it as a blueprint for a request that you can build once and reuse or inspect before sending.
Instead of:
fetch('https://api.example.com/data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
});
You can create a Request object:
const request = new Request('https://api.example.com/data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
});
fetch(request);
Key Configuration Options
The Request constructor accepts the same options you've used with fetch():
method: HTTP method like'GET','POST','PUT','DELETE'headers: An object orHeadersinstance with request headersbody: Request body (JSON string,FormData,Blob, etc.)mode: Controls cross-origin behavior ('cors','no-cors','same-origin')credentials: Whether to include cookies ('omit','same-origin','include')
const request = new Request('https://api.example.com/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice' }),
mode: 'cors',
credentials: 'include'
});
Why Use Request Objects?
They allow you to configure once, reuse multiple times, pass requests between functions, and inspect request properties before sending. It's especially useful when building API client libraries or managing complex request patterns.
Key Takeaway: The Request constructor creates a configurable, reusable request object with method, headers, body, mode, and credentials settings, offering better structure than passing options directly to fetch().