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

116
Visualizações
How can you read POST data from a javascript XMLHttpRequest in Golang?

Here's the javascript function called:

function cwk_submit_form() {
   var form = document.getElementById("FORM_ID")
   var XHR = new XMLHttpRequest();


   const FD = new FormData( form );
   for (const element of FD.entries()) {
       console.log(element)
   }

   XHR.open( "POST", "http://localhost:8080/post_data" );
   XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

   XHR.send( FD );
}

I left the console.log in there to mention that this does print out the correct data, meaning that the issue is seems to be in how the data is transferred.

The Golang code that receives the response is:

func post_data(w http.ResponseWriter, r *http.Request) {
    log.Println("post was called")

    r.ParseForm()
    for key, value := range r.Form {
        log.Printf("%s = %s\n", key, value)
    }
}

Nothing is printed by this for loop.

If I use an HTML Form to submit like so:

<form action="//localhost:8080/post_data" method="POST">
    <input type="text" name="field1" value="" maxLength="20"/>
        
    <input type="text" name="field2" value="" maxLength="20"/>
        
    <input type="submit" value="Sign in"/>
</form>

then the Golang code above works fine, which leads me to believe that the XMLHttpRequest format is the issue.

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

0

Your guess is right there is a problem in your js code.

For all requests, ParseForm parses the raw query from the URL and updates r.Form.

And hence, it will work when the Content-Type you send and the actual content type matches to application/x-www-form-urlencoded which happens in your HTML form case.

On the other hand, when you use FormData, it will be sent as multipart/form-data.

You need to replace your XHR.send(FD) with XHR.send(new URLSearchParams(FD)) in order to send the data in application/x-www-form-urlencoded.

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