#get data from form

7 messages · Page 1 of 1 (latest)

austere dew
#

Currently, calling formData seems to do nothing..

Workaround:


if (Astro.request.method === "POST") {
  const text = await Astro.request.text()

  const inputs = text
    .split("&").map((row) => [row.split("=")[0], decodeURIComponent(row.split("=")[1])])
  const inputJSON = Object.fromEntries(inputs)
  // now use inputJSON as an object with { inputName: inputValue }
  console.log(inputJSON)
}

Please keep in mind that this is a very basic approach to handling form data and many, many edge cases won't work (eg file uploads)

bold glade
#

First of all no stupid questions.

#

Would you post the entire content of your form?

hardy hatch
bold glade
#

Oh my client didn't load his answer yet, glad you got it sorted!

bold glade
#

I'm also implementing a form endpoint right now and actually you need to call .get(field) to get a field from the form. Logging a FormData object doesn't reveal anything for some reason (i guess because console.log doesn't display functions inside objects?). You don't have to parse the data manually

Snippet

...
const formData = await request.formData();
const password = formData.get("password");
...