Loading topic
fetch() is the built-in JavaScript function for making HTTP requests. You give it a URL, it sends the request, and it returns a Promise for the server's answer. That's the short version. The details matter, though. The Promise settles in a specific way and at a specific moment, and it settles on i6 min readfetch() settles, it gives you a Response object. Before you read any data from it, you should look at what kind of answer the server sent. This chapter covers the properties that describe a response: status, ok, statusText, headers, url, redirected, and type. Reading the body 6 min readfetch() resolves, you have a Response object, but the data the server sent is still not in your hands. The status line and headers arrive first. The body arrives separately, possibly in many pieces, and you decide how to read it. This chapter covers the body-reading methods, what each retur6 min readfetch(), inspect response.status and response.ok, and read a body with json() or text(). It also assumes you are comfortable with async/await, try/catch, and the meaning of common HTTP status codes such as 200, 404, and 57 min readfetch() and read what came back. Real applications also create, update, and delete data, send credentials in headers, and upload files. All of that goes through the second argument to fetch(), an options object usual6 min readContent-Type it sets for each one, and which mistakes cause a server to receive an empty or garbled payload.6 min readHeaders interface. The same interface appears in three places: as response.headers,6 min readfetch() keeps running until the server responds or the network fails. Sometimes you no longer want the result: the user typed another character into a search box, left the page, or the server is taking too long. This chapter shows how to cancel requests with `AbortControll6 min readfetch, check response.ok, set headers with the Headers interface, and send JSON bodies. You should also know what HTTP methods, status codes, and headers are, and how to open the browser's network panel. Here you will learn w6 min readresponse.json() and response.text() wait until the entire body has arrived before they give you anything. For a small API response that is fine. For a 200 MB log file, a long CSV export, or a server that sends results a line at a time, waiting means a frozen progress bar, high memory use, and no7 min readfetch on its own: checking response.ok, serializing JSON, building headers, adding timeouts, and reading bodies. In a real application, repeating all of that at every call site leads to duplicated code and small differences between call sites. This chapt6 min read