go – no match| OPTIONS beego-ThrowExceptions

Exception or error:

I have started learning beego.I have created an API that I call from an angular frontend.
To overcome CORS issues in my frontend I used the following code in my main.go file

package main

import (
    _ "gateway/routers"

    "github.com/astaxie/beego"
    "github.com/astaxie/beego/plugins/cors"
)

func main() {
    // Set CORS option for front-end applications
    beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
        AllowOrigins:     []string{"*"},
        AllowMethods:     []string{"OPTIONS","PUT", "PATCH", "POST", "GET", },
        AllowHeaders:     []string{"Origin"},
        ExposeHeaders:    []string{"Content-Length"},
        AllowCredentials: true,
    }))
    if beego.BConfig.RunMode == "dev" {
        beego.BConfig.WebConfig.DirectoryIndex = true
        beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
    }
    beego.Run()
}

Now I have the following router:

package routers

import (
    "gateway/controllers"

    "github.com/astaxie/beego"
)

func init() {
    ns := beego.NewNamespace("/v1",
        beego.NSNamespace("/info/host",
            beego.NSInclude(&controllers.HostController{})),
        beego.NSNamespace("/info/ifaces",
            beego.NSInclude(&controllers.NetworkController{})),
        beego.NSNamespace("/info/cpu",
            beego.NSInclude(&controllers.CpuController{})),
        beego.NSNamespace("/info/disk",
            beego.NSInclude(&controllers.DiskController{})),

        beego.NSNamespace("/info/memory",
            beego.NSInclude(&controllers.MemoryController{})),

        beego.NSNamespace("/info/users",
            beego.NSInclude(&controllers.UserController{})),

    )
    beego.AddNamespace(ns)
}

If i try to do a get request at “/v1/info/ifaces” I get the response from the corresponding controller.However If I do a post or put at the same error I get this:

::1| 200 | 41.587┬Ás| nomatch| OPTIONS /v1/info/ifaces

Any ideas why this is happening?

Also here is my NetworkController

package controllers

import (
    "encoding/json"
    "fmt"
    "github.com/astaxie/beego"
    "github.com/shirou/gopsutil/net"
)

// NetworkController operations for Network
type NetworkController struct {
    beego.Controller
}

// URLMapping ...
func (c *NetworkController) URLMapping() {
    c.Mapping("Post", c.Post)
    c.Mapping("GetOne", c.GetOne)
    c.Mapping("GetAll", c.GetAll)
    c.Mapping("Put", c.Put)
    c.Mapping("Delete", c.Delete)
}

// Post ...
// @Title Create
// @Description create Network
// @Param   body        body    models.Network  true        "body for Network content"
// @Success 201 {object} models.Network
// @Failure 403 body is empty
// @router / [post]
func (c *NetworkController) Post() {
    fmt.Println("Posting")
    c.ServeJSON()
}

// GetOne ...
// @Title GetOne
// @Description get Network by id
// @Param   id      path    string  true        "The key for staticblock"
// @Success 200 {object} models.Network
// @Failure 403 :id is empty
// @router /:id [get]
func (c *NetworkController) GetOne() {

}

// GetAll ...
// @Title GetAll
// @Description Retrieve network interfaces of gateway
// @Param   query   query   string  false   "Filter. e.g. col1:v1,col2:v2 ..."
// @Param   fields  query   string  false   "Fields returned. e.g. col1,col2 ..."
// @Param   sortby  query   string  false   "Sorted-by fields. e.g. col1,col2 ..."
// @Param   order   query   string  false   "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param   limit   query   string  false   "Limit the size of result set. Must be an integer"
// @Param   offset  query   string  false   "Start position of result set. Must be an integer"
// @Success 200 {object} models.Network
// @Failure 403
// @router / [get]
func (c *NetworkController) GetAll() {
    info, _ := net.Interfaces()
    c.Data["json"] = &info
    c.ServeJSON()
}

// Put ...
// @Title Put
// @Description update the Network
// @Param   id      path    string  true        "The id you want to update"
// @Param   body        body    models.Network  true        "body for Network content"
// @Success 200 {object} models.Network
// @Failure 403 :id is not int
// @router /:id [put,options]
func (c *NetworkController) Put() {
    var iface net.InterfaceStat
    _ = json.Unmarshal(c.Ctx.Input.RequestBody,&iface)
    fmt.Println(iface.Name)

}

// Delete ...
// @Title Delete
// @Description delete the Network
// @Param   id      path    string  true        "The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *NetworkController) Delete() {

}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *