Skip to content
tezvyn:

Configuring Fetch Requests with `RequestInit`

Source: developer.mozilla.orgEasyHow cards are made

Configuring Fetch Requests with `RequestInit`

RequestInit is the options object that customizes a fetch call beyond a simple GET. Use it to specify the HTTP method, send a request body, set headers, and control caching. A common footgun is sending a JSON body without setting the Content-Type header.

Why it exists

The fetch API needs a standard way to handle more than just simple GET requests. RequestInit provides a structured, extensible options object to configure every aspect of an HTTP request, from its method and body to complex caching and security policies.

The mental model

Think of RequestInit as the configuration panel for a fetch call. If fetch(url) is like typing an address into your browser, fetch(url, requestInit) is like using a developer tool to build a custom request, specifying the method, headers, body, and how the browser should handle caching.

How it works

You pass a RequestInit object as the second argument to fetch(). This object contains key-value pairs that map to HTTP request features. For example, method: 'POST' sets the method. body: JSON.stringify(data) provides the payload. headers: { 'Content-Type': 'application/json' } tells the server how to interpret the body. Other properties like cache control browser-level behavior, such as 'no-store' to bypass the cache entirely or 'reload' to fetch from the server but update the cache.

When to use it

Use RequestInit whenever you need more than a default GET. This is essential for sending data to a server (POST, PUT, PATCH), deleting a resource (DELETE), controlling caching to ensure data freshness or improve performance, or making authenticated requests by managing credentials.

When not to use it

For a simple, unauthenticated GET request where the browser's default caching behavior is acceptable, you can omit the RequestInit object. Just passing the URL to fetch() is sufficient, as the browser uses sensible defaults.

One canonical example

To create a new user via a JSON API, you'd use a POST request. You would create an options object setting the method to 'POST', the headers to include 'Content-Type': 'application/json', and the body to the JSON.stringify()-ed user data. This object is then passed as the second argument to fetch('https://api.example.com/users', options).

Interview question

When sending a JavaScript object as JSON data in a POST request using fetch and RequestInit, what is a crucial configuration step?

  • a.Specifying the 'Content-Type' header as 'application/json'.Correct
  • b.Omitting the 'body' property, as fetch infers it from the method.
  • c.Setting the 'method' property to 'GET' to ensure data is sent.
  • d.Automatically converting the JavaScript object to a JSON string within RequestInit.
Why?

The card explicitly states that a common pitfall is sending a JSON body without setting the 'Content-Type' header to 'application/json', which is crucial for the server to correctly interpret the data. Option D is incorrect because RequestInit itself does not perform JSON stringification; you must use JSON.stringify() explicitly for the 'body' property.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles