Exposing API endpoints in Wails with Axios (404)

53 Views Asked by At

I'm creating a desktop app using Wails with React.JS, I have a simple endpoint I want to expose, I'm using Axios to make requests from the React.JS Component, in the backend, I'm using echo to expose the endpoint.

package main

var assets embed.FS

func main() {
    app := NewApp()

    b := new(traffic.ErlangB

    go func() {
        e := echo.New()
        e.Use(middleware.CORS())

        e.POST("/traffic/calculate-traffic", handlers.Calc_traffic_Handler)
        e.Start(":5173")
    }()

    err := wails.Run(&options.App{
        Title:      "My App",
        Width:      620,
        Height:     641,
        Fullscreen: false,

        AssetServer: &assetserver.Options{
            Assets: assets,
        },
        BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
        OnStartup:        app.startup,
        Bind: []interface{}{
            app,
            b,
        },
    })
    if err != nil {
        println("Error:", err.Error())
    }

}

My Handler functions

func Calc_traffic_Handler(c echo.Context) error {
    var params struct {
        GoS             float64
        Num_of_channels int
    }
    if err := c.Bind(&params); err != nil {
        return echo.NewHTTPError(422, "Invalid Request Body")

    }
    b := traffic.ErlangB{}
    result := traffic.ErlangB.Calc_traffic(b, params.GoS, params.Num_of_channels)

    return c.JSON(http.StatusOK, result)
}

Axios Code in the frontend

  const handleClick = async () => {
    console.log(num_of_channels, gos)
    try {
      const res = await axios.post(
        "http://localhost:5173/traffic/calculate-traffic",
        {
          gos: gos,
          num_of_channels: num_of_channels,
        }
      ).then(res => console.log(res)).catch(err => console.error("Axios Errorrr: ", err))
      setTraffic(res.data);
    } catch (error) {
      console.error("Error: " + error);
    }
  };

the problem is that I always get 404 error, when I run it using Postman or using the UI. Axios simply returns

POST http://localhost:5173/traffic/calculate-traffic 404 (Not Found)
1

There are 1 best solutions below

0
Toastlover On

Use stdlib instead. More Info: https://www.youtube.com/watch?v=JECZTdEJnOI