Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Re assigning value of formData to a variable is returning an empty object in the backend

I am using formData with axios in my react frontend app in order to accommodate the image field. Tho, the data that I sent to the backend Django rest app is empty.

Frontend axios request

const addPost = (userInput, redirectTo) => dispatch => {


   const input = [];
   
   input[0] = userInput[0];
   input[1] = userInput[1];

   const postInput = userInput[2];


    const formData = new FormData();

    formData.append("title", postInput.title);
    formData.append("excerpt", postInput.excerpt);
    formData.append("content", postInput.content);
    formData.append("status", postInput.status);
    formData.append("image", postInput.image);
    formData.append("created_by", postInput.created_by);
    formData.append("category", postInput.category);
    formData.append("slug", "");

    input[2] = formData;

    axios
    .post(POST_ADD_URL, input)
    .then(response => {
         dispatch({type: ADD_POST_SUCCESS})
           toast.success(
                  " added successfully!"
              )
         dispatch(getAllPost())
         dispatch(push(redirectTo))
    })
    .catch(error => {
         dispatch({type: ADD_POST_FAIL})
         errorFilter(error)
         dispatch(push(redirectTo))
    })

}

This is mainly because I assigned the value of "formData to another variable "input1 = formData;

Backend code

    def create(self, request):

    # filter data
    data = request.data

    # filter user by email
    author = data[0]

    # filter category
    category = data[1]

    # filter post
    post = data[2]

    print("\npost data\n")
    print(post)
    print("\n\n")

    serializer = PostSerializer(data=post)
    serializer.is_valid(raise_exception=True)

    # filter instances
    user_instance = get_object_or_404(User, email=author['email'])

    category_instance = get_object_or_404(Category, name=category['category'])

    serializer.save(category=category_instance, created_by=user_instance)
    return Response(serializer.data, status=status.HTTP_201_CREATED)

enter image description here enter image description here

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is happening because axios converts the input array to json and sets the content-type of the request to application/json. Converting FormData to json will return an empty object. If you want to send FormData though a request use

 // append the userInputs in the formData
 axios.post(url, formData);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda