I am creating a HTTP fullstack website with my backend using django-rest-framework.
but, recently on front-end i created a function of route in this form:
export const createExternalUnity = async (unityData: IGetUnity) => {
try {
const response = await api.post('/unidades/', unityData)
return response.data as IGetUnity
} catch (error) {
console.error(error)
alert(`Erro ao criar uma nova unidade, devido ao erro ${error as Error}`)
}
}
and onSubmit logic of the form:
<form
onSubmit={handleSubmitUnity((formData: IGetUnity) => {
if(selectedAtivoOp.id) {
updateExternalUnity(selectedAtivoOp.id, formData)
.then(() => {
toast.success(`Unidade atualizada com sucesso!`, {
position: 'top-left',
duration: 4000,
})
})
.catch(error => (
toast.error(`Erro inesperado, ${error}`, {
position: 'top-left',
duration: 4000,
})
))
} else {
formData.id = getNewId().toString()
createExternalUnity(formData)
.then(() => {
toast.success(`Unidade criada com sucesso!`, {
position: 'top-left',
duration: 4000,
})
}
).catch(error => (
toast.error(`Erro inesperado, ${error}`, {
position: 'top-left',
duration: 4000,
})
))
}
})}
but the response on the post method it's only Bad Request - 400, but the other methods are working correctly. Why this keep ocurring?