Search code examples
reactjsapistrapi

Strapi POST API call issue


Why Strapi POST Call, always expecting Multi-Part request I Don't have multi-part data in the particular content Type.

In Strapi CMS Portal, Roles & permission section, I have given access to CREATE

Post Data enter image description here

Response enter image description here

import React, { useState } from 'react'

export default () => {
    const [description, setDescription] = useState('')

    const handleSubmit = async (event) => {
        event.preventDefault();

        const data = await fetch('http://localhost:1337/posts', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                description
            })
        })
        const response = await data.json();
        console.log(response)
    }
    return (
        <div className="Create">
            <form onSubmit={handleSubmit}>
                <input
                    type="text"
                    placeholder="Description"
                    value={description}
                    onChange={(event) => {
                        setDescription(event.target.value)
                    }}
                />
                <button>Submit</button>
            </form>
        </div>
    )
}

Solution

  • Technology stack which I'm using in this project

    Frontend: Reactjs Backend: Strapi

    When I face this issue, I may not be aware of it. What is happening in the backend(Strapi).

    Thinks found after I face this issue:

    1. When you create a new collection type in strapi CMS. Strapi will automatically create 4 Folders [config, controllers, models, services], inside the api folder, for customization purposes.
    2. The issue, which I faced is based on the conditions which are added in the controller's folder file (post.js), then I removed that code & tried to hit that POST API call, it's working fine. Without any error.

    Helpful Resource, i found in strapi documentaion:

    https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#controllers

    Strapi Create Customization code file in the controller file. enter image description here