Like what we’re doing? Star us on GitHub!

Unable to save document (form submissions) with REST API

lksfrn
7 months ago
1 1

Hi,
I tried payloadcms/plugin-form-builder and I have some questions. How to create form submissions? Should I point my HTML directly to Payload? Richt now, I have proxy function that process data and after that I tried to create new form submission using REST API.

const formSubmission = {
  form: '<some-mongo-id>',
  submissionData: [
    { field: ...', value: '...' },
    ...
}
const res = await apiFetch('<api-url>/form-submissions', {
  body: JSON.stringify(formSubmission),
  method: 'POST',
})
const formData = new FormData()
formData.append('_payload', JSON.stringify(formSubmission))

const res = await apiFetch('<api-url>/form-submissions', {
  body: formData,
  headers: {
    'Content-Type': 'multipart/form-data',
  },
  method: 'POST',
})

Neither of these approaches work. They return validation error that prop form is missing, Do you have any ideas what I am doing wrong? I think the documentation lacks details on how to work with REST mutations.

Thanks!

  • JarrodMFlesch
    Payload Team
    7 months ago

    That should work...

    Here is a snippet pulled from a site my team built.

    const dataToSend = Object.entries(data).map(([name, value]) => ({
      field: name,
      value
    }));
    
    const req = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/form-submissions`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        form: formID,
        submissionData: dataToSend
      })
    })
    1 reply
  • lksfrn
    7 months ago

    Thank you, I think the problem was that I did not specified Content-Type. Dummy mistake!

Open the post
Continue the discussion in GitHub
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More