How to Create APIRest With Go and ECHO Framework?

0

I am trying to do a APIRest with the framework ECHO for the language GO , but I find the following difficulty, what I want is to make a request fetch with javascript , in where I send in the header of an invoice and its respective detail, the problem is that I do not know how to go through the data, my code is as follows:

HTML:

   <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>

<body>
    <div id="msj"></div>
<script>
    var detalles = [];
    var detalle = {
        Codigo      : '123',
        Descripcion : 'Azucar',
        Cantidad    : '1',
        Valor       : '5'
    };
    detalles.push(detalle);
    var detalle = {
        Codigo      : '321',
        Descripcion : 'Arroz',
        Cantidad    : '2',
        Valor       : '1'
    };
    detalles.push(detalle);

    var datos ={ 
        "Cliente"       :'Andres', 
        "Direccion"     :'Calle Falsa 123', 
        "Email"         :'[email protected]', 
        "Detalles"      : detalles,
    }

    var url1 = "http://localhost:1323/"
    fetch(url1,
    {
        headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
        },
        method: "POST",
        body: JSON.stringify(datos)
    })
    .then(function(res){ return res.json(); })
    .then(function(data){ document.getElementById("msj").innerHTML = JSON.stringify( data );  })
    .catch(function(error) {
    console.log('Hubo un problema con la petición Fetch:' + error);
    });
</script>
</body>

</html>

CODE GO:

package main

import (
    "net/http"

    "github.com/labstack/echo"
    "github.com/labstack/echo/middleware"
)

type Detalle struct {
    Codigo      string 'json:"codigo" form:"codigo" query:"codigo"'
    Descripcion string 'json:"descripcion" form:"descripcion" query:"descripcion"'
    Cantidad    string 'json:"cantidad" form:"cantidad" query:"cantidad"'
    Valor       string 'json:"valor" form:"valor" query:"valor"'
}
type Factura struct {
    Cliente   string 'json:"cliente" form:"cliente" query:"cliente"'
    Direccion string 'json:"direccion" form:"direccion" query:"direccion"'
    Email     string 'json:"email" form:"email" query:"email"'
    Detalles  []Detalle
}

func main() {
    e := echo.New()
    e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
        AllowOrigins: []string{"*"},
        AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE},
    }))
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    e.POST("/", save)
    e.Logger.Fatal(e.Start(":1323"))
}

func save(c echo.Context) (err error) {
    u := new(Factura)

    if err = c.Bind(u); err != nil {
        return c.String(http.StatusInternalServerError, "Error")
    }
    return c.JSON(http.StatusOK, u)
}

I am trying to guide me to the documentation but I still can not achieve it, the idea is to go through the detail to do the insertion in the BD.

    
asked by Andrés 08.10.2018 в 23:54
source

0 answers