liveBook · Manning

HTTP request

As explained in Acing the Frontend Interview

POST is an HTTP method for submitting data in the request body, commonly for operations that may create or change server state.

When a form submits data with POST, the request message carries the encoded form data to the server. A useful interview answer traces that request beyond the browser: the server validates and processes the data, sends a response, and the browser displays success or errors. section 2.4

To explain where the request fits, distinguish these steps:

  • Validate input on the client, then collect and encode the form fields according to the method and content type. section 2.4
  • Send the request; validate again on the server before processing the data. section 2.4
Listing 2.8 Simple form implementation
const Form: React.FC = () => {
  const [state, formAction, isPending] = useActionState<FormState, FormData>( #A
    async (prevState, formData: FormData) => { #B
      try {
        const response = await fetch('https://api.web3forms.com/submit', { #C
          method: 'POST',
          body: formData as BodyInit, #D
        });
        const data = await response.json(); #E
        if (!data.success) { #F
          throw new Error(data.message);
        }
        return { #G
          data,
          error: null,
        };
      } catch (error) { #H
        return {
          data: null,
          error: `There was a problem with your submission: ${error}`,
        };
      }
    },
    { data: null, error: null } #I
  );
  return (
    <form action={formAction}> #J
      <input #K
        type="hidden"
        name="access_key"
        value={import.meta.env.VITE_ACCESS_KEY} #L
      />
      <label htmlFor="name">Name:</label>
      <input #M
        type="text"
        name="name"
        required
        pattern="[A-Za-z\s]+" #N
        title="Only letters and spaces allowed."
      />
      <label htmlFor="email">Email:</label>
      <input type="email" name="email" required /> #O
      <input type="submit" value="Submit" disabled={isPending} /> #P
      {state.error && <div>{state.error}</div>} #Q
      {state.data && ( #R
        <div>Form submitted successfully: ${JSON.stringify(state.data)}</div>
      )}
    </form>
  );
}

The React example sends FormData with fetch using method: 'POST', then handles either the returned success data or a submission error. section 2.4

How Acing the Frontend Interview covers it

How deep it goes
Traces the submission sequence and shows a POST fetch, without detailing HTTP message internals.
Examples
Shows a React form sending FormData through a POST fetch and handling success or error.
What it assumes
Assumes familiarity with HTML form controls and JavaScript or TypeScript while spelling out the submission steps.
What you can do after
Apply it via fetch to submit form data and explain the resulting request–response flow.
How it connects
Connects HTML forms, FormData, client-side validation, server-side validation, and HTTP responses.

Also explained in

Compare how 18 books explain HTTP request →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Frontend Interview. The section links point to the parts of the book it draws on.