Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 39 ยท Getting Started

Web Fundamentals in Remix: Request, Response, FormData

Learn the web platform APIs Remix is built on: URL, URLSearchParams, Request, Response, Headers and FormData, with runnable examples that mirror loaders and actions.

Remix does not invent its own request and response objects. Loaders and actions receive a standard Request and return data or a standard Response. The same classes exist in browsers, Node.js 18+, Deno, Bun and Cloudflare Workers. If you know them, you already know most of the framework's server API. If you do not, this lesson is the place to learn them, because every later lesson uses them.

Every example here runs as-is. They use only built-in classes, so the stand-ins are not stand-ins at all: this is exactly what your loaders and actions will do.

URL and URLSearchParams

A loader gets the full address in request.url, which is a string. Wrap it in URL to take it apart.

JS Taking a URL apart

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Three things to remember:

  • get returns string | null. Search parameters are always strings; convert with Number(...) and give a default.
  • getAll returns every value of a repeated key. Use it for multi-select filters.
  • The #hash never reaches the server. Only the browser sees it.

Request

A Request carries the method, URL, headers and an optional body.

JS Reading a Request

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last lines show the most common beginner mistake: a request body is a stream that can be read once. If you need to read it twice (for logging, say), call request.clone() before reading.

FormData

When a form posts, a loader or action reads it with await request.formData(). This is the entry point of nearly every Remix mutation. You can build one by hand too, which is useful in tests.

JS FormData basics

Press Run (or Ctrl + Enter) to see the console output here.

No output.

form.get returns FormDataEntryValue | null, which is string | File | null. TypeScript will make you check typeof value === "string", and that check is a good habit even in plain JavaScript. A browser only sends successful controls: an unchecked checkbox is simply absent, not false.

Response and Headers

A loader can return a plain object (the framework serialises it), or a Response when you need control over status codes and headers.

JS Building responses

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice Set-Cookie: unlike other headers, it must not be joined with commas, so use headers.getSetCookie() or append for each cookie. Sessions in a later chapter rely on that.

In React Router v7 you rarely build redirects by hand; the redirect() helper creates the response with the right header. The data() helper creates responses with a status for errors such as 404. Both are covered in the loaders and actions lessons.

Status codes you will use

Status Meaning Typical use in a route
200 OK a normal page
201 Created an action created something
301 / 302 / 303 Redirect after a successful form post use 303 or the default redirect
400 Bad request validation failed
401 / 403 Not signed in / not allowed auth guards
404 Not found missing record
500 Server error unexpected exception

Why this matters for Remix

The platform is the API

URL Path, params, search
Request Method, headers, body
FormData What forms post
Response Status, headers, body

Learn these once, use them on every runtime.

1 / 2

Because loaders and actions use the standard classes, you can test them without a server: build a Request, call the function, inspect the result. The rest of this track relies on that. The JavaScript tutorial covers fetch if you want a refresher on the client side of the same classes.

Common mistakes

  • Reading request.formData() twice (the body is already consumed).
  • Treating searchParams.get("page") as a number (it is a string or null).
  • Using Object.fromEntries(formData) on forms with repeated names (you lose all but the last).
  • Appending multiple cookies to one Set-Cookie string; append each separately.
  • Forgetting that header names are case-insensitive but cookie names are not.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does new URL(request.url).searchParams.get('page') return when the URL has no page parameter?

    URLSearchParams.get returns null for a missing key, so convert with a default: Number(params.get('page') ?? '1').
  2. 2. Why does reading await request.text() twice throw?

    Use request.clone() before reading if you need the body twice.
  3. 3. A form contains an unchecked checkbox named done. What does formData.get('done') return?

    Browsers do not send unchecked checkboxes, so the field is absent and get returns null.
  4. 4. Which method returns every value of a repeated search parameter such as ?tag=a&tag=b?

    get returns only the first value; getAll returns an array of all of them.

Summary

  • Loaders and actions work with standard Request and Response objects.
  • Use new URL(request.url) and searchParams for query data; values are strings or null.
  • await request.formData() reads posted forms; validate every field.
  • Response.json, Headers, and status codes give you precise control; Set-Cookie needs separate entries.
  • You can test loaders and actions by building requests by hand.

Next: Routes and Route Modules