Write a createUser function that POSTs JSON via fetch

precise fetch configuration for JSON POST requests. A strong answer names method POST, headers Content-Type application/json, and body JSON.stringify(data) with return typing. Red flag: passing the raw object as body or omitting headers.
What's really being asked
This question checks whether you can configure a modern fetch call for a JSON POST endpoint without relying on a library like axios. Interviewers want to see that you know fetch defaults to GET, that you must explicitly set the method to POST, and that you understand the separation between the network Response object and the actual parsed JSON body. At the senior level it also probes whether you think about error handling, since fetch promises resolve even on HTTP 404 or 500 status codes.
The full answer
First, the method option must be set to POST because fetch uses GET by default. Second, the headers option must include Content-Type set to application/json so the server knows how to parse the payload. Third, the body option must be JSON.stringify(data) because fetch does not automatically serialize objects; passing the raw object results in the string object Object. Fourth, you should await the fetch call, then check response.ok or response.status before calling response.json() to parse the returned data. Fifth, the function should be typed to return Promise of any or a proper user interface rather than the raw Response.
The mistakes people make
Passing the data object directly into the body field without JSON.stringify is the most frequent mistake and silently breaks the request. Omitting the Content-Type header is another red flag because many APIs will reject the request or fail to parse the payload. Returning the raw Response object instead of awaiting response.json() misses the point of the function signature which asks for the parsed result. Using fetch without checking response.ok is also a red flag because fetch only rejects on network failure, not on HTTP error codes.
What usually comes next
The interviewer might ask how you would handle network errors versus HTTP 4xx or 5xx errors. They might also ask what happens if you forget to call JSON.stringify, or how you would strongly type the return value instead of using Promise of any. Another common follow-up is how to abort a request using an AbortController signal, or how to handle timeouts since fetch has no built-in timeout option.
A concrete example
A solid implementation looks like this. Declare an async function named createUser that accepts data of type name string email string. Call fetch with the url slash api slash users and an options object. Inside the options object set method to POST, set headers to a new object with Content-Type equal to application json, and set body to JSON dot stringify of data. Await the fetch call and store the response. If response dot ok is false throw a new error with the status text. Otherwise return await response dot json. This gives you proper serialization, correct headers, and basic error handling in under ten lines.
Interview question
When configuring fetch inside an async createUser function to POST JSON data, which option correctly serializes the payload and handles the response?
- a.body is JSON.stringify(data), Content-Type is application/json, and it returns the raw Response object
- b.body is JSON.stringify(data), Content-Type is application/json, it checks response.ok, and returns await response.json()Correct
- c.body is the raw data object, Content-Type is application/json, and it returns await response.json()
- d.body is JSON.stringify(data), the Content-Type header is omitted, and it returns await response.json()
Why? this is the answer
Option B is correct because fetch requires JSON.stringify to serialize the payload and a Content-Type header so the server can parse it, plus it returns the parsed JSON rather than the raw Response. Option C is tempting but wrong because passing the raw object directly causes the body to become the string [object Object], silently breaking the request.
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.
We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.
See open roles