strapi / starters-and-templates

Monorepo for all official Strapi v4 templates
MIT License
327 stars 117 forks source link

Bug in lead-form.js : JSON data sent to server is malformed #53

Open webhype opened 2 years ago

webhype commented 2 years ago

Email lead form submissions fail in the Corporate Starter Template because the JSON data sent to http://localhost:1337/api/lead-form-submissions isn't wrapped in { data: { <payload> }}, leading to 400 BAD REQUEST errors.

Buggy code:

await fetchAPI(
    "/lead-form-submissions",
    {},
    {
        method: "POST",
        body: JSON.stringify({
            email: values.email,
            location: data.location,
        }),
    }
)

Fixed code:

await fetchAPI(
    "/lead-form-submissions",
    {},
    {
        method: "POST",
        body: JSON.stringify({
            data: {
                email: values.email,
                location: data.location,
            }
        }),
    }
)

At first I couldn't replicate this behavior with Postman either. Turns out Strapi expects its POSTDATA in a very peculiar way (at least peculiar to me 😂).

Mad props to Arsiki at https://stackoverflow.com/questions/71187083/problem-with-post-action-on-strapi-via-postman for figuring out how to send POSTDATA to Strapi via Postman. (I am sure the proper way is documented somewhere also.)

After that, it became clear that the data field was missing in the JSON submitted back home via the lead form.