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!
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
})
})
Thank you, I think the problem was that I did not specified Content-Type
. Dummy mistake!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.