#error when handling with POST form

2 messages · Page 1 of 1 (latest)

brave holly
#

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?

shrewd turret
#

An HTTP 400 code means the endpoint works but doesn't get the expected data.

There could be a difference between the fields that are present in the payload versus the fields the endpoint wants.

There could also be a malformed field.

In case of validation errors, you should get these errors in the response body of your request.

You could also temporarily adjust your Python code to print errors to the console.
Since we don't have your Python code, I cannot make better suggestions to that.