parameters React.js to GO/Golang

Sending parameters to GO/Golang, follow the code below:
const params = JSON.stringify({
“administrador”: administrador,
“ativo”: ativo,
“dtCotas”: dtCotas,
“saldoInicial”: saldoInicial,
“vlrCotas”: vlrCotas,
“qtdeCotas”: qtdeCotas,
“dividendosCotas”: dividendosCotas,
“dividendos”: dividendos,
“qtdeNovasCotas”: qtdeNovasCotas,
“sobra”: sobra,
});

axios.post (endpoint + "/api/insertOneInvestir/" + params, {
  headers: {
    "content-type": "application/json"
    // "Content-Type": "application/x-www-form-urlencoded"
  }
}) 

I can not recover the data (params) in the backend (GO), does not display error, writes to the database (MongoDB) record (collection) empty; the code below:
router.HandleFunc("/api/insertOneInvestir/{params}", middleware.CreateInvestir).Methods(“POST”, “OPTIONS”)


func CreateInvestir(w http.ResponseWriter, r http.Request) {
w.Header().Set(“Context-Type”, “application/x-www-form-urlencoded”)
w.Header().Set(“Access-Control-Allow-Origin”, "
")
w.Header().Set(“Access-Control-Allow-Methods”, “POST”)
w.Header().Set(“Access-Control-Allow-Headers”, “Content-Type”)

params := mux.Vars(r)
var invest fundosinvestir
_ = json.NewDecoder(r.Body).Decode(&invest)
/*
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&invest)

	if err != nil {
		panic(err)
	}
	----------------------------------------------
		_ = json.NewDecoder(r.Body).Decode(&invest)
		//emps = append(emps, invest)
		insertOneInvestir(invest)
		json.NewEncoder(w).Encode(invest)
*/
/*
		params := mux.Vars(r)
	    var person Person
	    _ = json.NewDecoder(r.Body).Decode(&person)
	    person.ID = params["id"]
	    people = append(people, person)
		json.NewEncoder(w).Encode(people)

*/
/*
	fmt.Println(params["saldoInicial"])
	fmt.Println(params["vlrCotas"])
	fmt.Println(params["qtdeCotas"])
	fmt.Println(params["dividendosCotas"])
	fmt.Println(params["dividendos"])
	fmt.Println(params["qtdeNovasCotas"])
	fmt.Println(params["dividendos"])
	fmt.Println(params["qtdeNovasCotas"])
	fmt.Println(params["sobra"])
*/
administrador := params["administrador"]
ativo := params["ativo"]
dtCotas := params["dtCotas"]
saldoInicial, _ := strconv.ParseFloat(params["saldoInicial"], 64)
fmt.Println(saldoInicial)
vlrCotas, _ := strconv.ParseFloat(params["vlrCotas"], 64)
fmt.Println(vlrCotas)
qtdeCotas, _ := strconv.Atoi(params["qtdeCotas"])
dividendosCotas, _ := strconv.ParseFloat(params["dividendosCotas"], 64)
dividendos, _ := strconv.ParseFloat(params["dividendos"], 64)
qtdeNovasCotas, _ := strconv.Atoi(params["qtdeNovasCotas"])
fmt.Println(qtdeNovasCotas)
sobra, _ := strconv.ParseFloat(params["sobra"], 64)
fmt.Println(sobra)
investirid := "5f9eaba56f323af8e6e91193"

invest.Administrador = administrador
invest.Ativo = ativo
invest.DtCotas = dtCotas
invest.SaldoInicial = saldoInicial
invest.VlrCotas = vlrCotas
invest.QtdeCotas = qtdeCotas
invest.DividendosCotas = dividendosCotas
invest.Dividendos = dividendos
invest.QtdeNovasCotas = qtdeNovasCotas
invest.Sobra = sobra
invest.Investirid = investirid

// fmt.Println(invest.Administrador, r.Body)
insertOneInvestir(invest)
json.NewEncoder(w).Encode(invest)

}

This will make your full URL something like

https://myendpoint.com/api/insertOneInvestir/{
“administrador”: "algún administrador",
“ativo”: true,
“dtCotas”: "algunas dtCotas",
...
}

Which doesn’t look right.

The code in CreateInvestir has the JSON decoding commented out and looks like it instead uses the x-www-form-urlencoded vars from mux, but your call to axios.post looks like it’s trying to encode its parameters as json. I’m not sure which code is right, but after you decide on that, I think you just need to put the params somewhere in the body of your axios.post call (is there a body key at the same level as the headers key?)

If you go with the URL-encoded values, then you’ll first have to translate your params into a URL-encoded set of key-value pairs instead of JSON.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.