Envio de parâmetros para GO/Golang.

54 views
Skip to first unread message

Wilson Palmeira Júnior

unread,
Dec 18, 2020, 7:12:24 AM12/18/20
to Golang Brasil

Não consigo recuperar os dados (params) no backend (GO), não exibe erro, grava para o banco de dados (MongoDB) registro (coleção) vazio; segue o código abaixo:
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)

}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)

}

------------------------
Envio de parâmetros para GO/Golang, segue o código abaixo:
React.js
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"
      }
    }) 
-------------------
Muito obrigado,

Wilson Palmeira
(21) 98577-2172
Reply all
Reply to author
Forward
0 new messages