The Request Constructor and Options
You'll learn how to build Request objects from scratch, giving you precise control over every aspect of your HTTP requests before sending them with fetch().
Until now, you've been passing URLs and options directly to fetch(). But JavaScript provides a Request constructor that lets you build a request object separately, configure it exactly how you need, and then send it—making your code more organized and reusable.
Why Create Request Objects?
Think of the Request constructor as a blueprint. Instead of configuring your fetch call inline every time, you create a request "template" that captures all the details: URL, method, headers, and body. You can inspect it, pass it around, or even clone it before sending.
Basic Syntax
const request = new Request('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token123'
},
body: JSON.stringify({ name: 'Alice' })
});
// Now send it
const response = await fetch(request);
Common Options
The second argument accepts the same options you've used with fetch():
method: GET, POST, PUT, DELETE, etc.headers: An object or Headers instancebody: String, FormData, or JSON stringmode,credentials,cache: Advanced control options
You can also create a Request without options (new Request(url)) and it defaults to a GET request.
Why This Matters
Creating Request objects separates what you're requesting from when you send it. This is valuable for logging, middleware patterns, or building request factories in larger applications.
Key Takeaway: The Request constructor lets you assemble HTTP requests as objects with full control over URL, method, headers, and body—improving code organization and reusability.