I’m trying to postdata with addForm object with todo input.
Input data:
<input
name="todo"
value={addForm.todo}
onChange={typer}
type="text"
className="form-control"
required
/>
<button
type="button"
onClick={addItem}
className="btn btn-primary mt-2"
>
Add
</button>
Add data:
axios.defaults.baseURL = “http://localhost:1337/”;
axios.defaults.headers[“Content-Type”] = “application/json”;
const [addForm, setAddForm] = useState({todo:""});
const [list, setList] = useState<TodoList[]>([]);
const addItem = async (): Promise<void> => {
try {
setLoading(true);
const { data } = await axios.post<TodoList[] | TodoList>("todos", addForm);
setList([...list, data] as TodoList[]);
} catch (err) {
console.log(err.message);
}
};
When i get data from Strapi API it works so well same with delete function but not with POST data.
When i sent request i get this request from api:
[
{
“_id”: “60ce7cfd99d42731cc2a1e91”,
“published_at”: “2021-06-19T23:25:50.550Z”,
“createdAt”: “2021-06-19T23:25:49.227Z”,
“updatedAt”: “2021-06-19T23:25:50.565Z”,
“__v”: 0,
“id”: “60ce7cfd99d42731cc2a1e91”
}
]
but i have Content field in api as not exists every time i post data. when i get data Content exists in data but not when i post data.
If i change postdata from addForm to addForm.todo it throw this error: POST 400(bad request) but when i send addForm it dosen’t throw errors but dosen’t send Content. Please help…